Nanu
Nanu

Reputation: 3070

How to pass value of Html.Dropdownlist to Controller

I am dynamically generating a dropdownbox. I am trying to send the selected value in dropdown box as one of the fields of a model to controller.

@using (Html.BeginForm("AddItem", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <label>
            Category:</label>
        @Html.DropDownList("Category", (IEnumerable<SelectListItem>)ViewData["CategoryList"])<br />
        <label>
            Sku:</label>
        @Html.TextBox("newItem.Sku")<br />
        <label>
            Title:</label>
        @Html.TextBox("newItem.Title")<br />

I am able to send all the values as a part of model, except the value of Category(dropdownbox value), hence making the function in controller to fail..

ANSWER: Renaming the Dropdownlist "Category" to = "newItem.Category", did the work, basically it should match the model name.

Upvotes: 1

Views: 13992

Answers (3)

Nanu
Nanu

Reputation: 3070

Renaming the Dropdownlist "Category" to = "newItem.Category", did the work, basically if you expect a model to be received in controller, which in this case was "newItem", down control name should match the model name and its property.

Upvotes: 0

Shyju
Shyju

Reputation: 218732

Create a ViewModel for your Item with Properties to hold all Categories and SelectedCategoryId value

public class ItemViewModel
{
  public int ItemId { set;get;}
  public string SKU { set;get;}
  public string SelectedCategoryId { get; set; }
  public IEnumerable<SelectListItem> Categories{ get; set; }
}

In your home controller, Create Action method for Add where you create an object of ItemViewModel, Set the Categories and return to the View which is strongly typed.

public ActionResult AddItem()
{
  ItemViewModel objNewItem=new ItemViewModel();
  objNewItem.Items = new[]
  {
     new SelectListItem { Value = "1", Text = "Perfume" },
     new SelectListItem { Value = "3", Text = "Shoe" },
     new SelectListItem { Value = "3", Text = "Shirt" }
  };
  return View(objNewItem);  

}

The Strongly typed View

@model ItemViewModel
 @using (Html.BeginForm("AddItem", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
 {
   Category:
   @Html.DropDownListFor(x => x.SelectedCategoryId ,new SelectList(Model.Categories,"Value",Text"), "Select..")
   <input type="submit" value="Add" />
 }

And have the HTTPPOST AddItem method in your Home Controller

[HttpPost]
public ActionResult AddItem(ItemViewModel objItem)
{
  //Now you can access objItem.SelectedCategoryId and do what you like to do...

}

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

Your DropDown is bound to a field called Category. So you must have such field on your view model.

public class MyViewModel
{
    public string Category { get; set; }
    ...
}

and your action:

[HttpPost]
public ActionResult AddItem(MyViewModel model)
{
    // model.Category will contain the selected value
    ...
}

Also note that this property must be a simple scalar value (string, int, ...) and not a complex object.

And once you have an adapted view model you could use the strongly typed versions of the helpers:

@model MyViewModel
@using (Html.BeginForm("AddItem", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.LabelFor(x => x.Category)
    @Html.DropDownListFor(x => x.Category, Model.Categories)
    <br />
    @Html.EditorFor(x => x.Sku)
    <br/>
    @Html.EditorFor(x => x.Title)
    ...
}

Upvotes: 2

Related Questions