1

I have a create page and have dropdownlist. My problem is that when I submit the form the dropdownlist is null. Help please. Here's my code.

Class

 public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public List<SelectListItem> Types { get; set; }
    }

Create (GET)

        // GET: /Products/Create

        public ActionResult Create(string id)
        {
            List<Product> products;
            if (Session["products"] != null) products = Session["products"] as List<Product>;
            else products = new List<Product>();
            var product = products.FirstOrDefault(x => id != null && x.Id == int.Parse(id))??new Product();
            var types = new List<SelectListItem>();
            var type = new SelectListItem { Selected = false, Value = "0", Text = "Electronics" };
            types.Add(type);
            type = new SelectListItem { Selected = false, Value = "1", Text = "Non-Electronics" };
            types.Add(type);
            product.Types = types.Select(x => new SelectListItem { Value = x.Value, Text = x.Text }).ToList();
            return View(product);
        } 

View

@model MVCViewState.Models.Product

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Product</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Types)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.Types, Model.Types)
            @Html.ValidationMessageFor(model => model.Types)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Create (POST)

enter image description here enter image description here

1 Answer 1

4

I think your intention is to capture the value of the selected Type. So you need something like this:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public List<SelectListItem> Types { get; set; }
    public int TypeId { get; set; } // the selected type from the dropdown
}

And on your view you need this:

 @Html.DropDownListFor(model => model.TypeId, Model.Types)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.