I have a simple form in Asp.Net MVC form and I had a dropdown list with values pulled from a view data. How can I get the value of selected value from dropdown to the controller object to use inside httppost method?
@Html.LabelFor(model => model.ItemType)
@Html.DropDownList("ItemTypes", new SelectList((IEnumerable)ViewData["itemTypes"], "Value", "Text"))
<input type="submit" value="Request Item" />
ViewData["itemTypes"] = GetItemTypes();
[HttpPost]
public ActionResult NewVc(ItemInfoViewModel itemInfoObject)
{
//Item not populated to itemInfoObject
}
Pull Item types from
public static List<ListItem> GetItemTypes()
{
var itemTypes = new List<ListItem>
{
new ListItem {Text = "Select", Value = ""},
new ListItem {Text = "Item1", Value = "Item1"},
new ListItem {Text = "Item2", Value = "Item2"},
new ListItem {Text = "Item3", Value = "Item3"}
};
return itemTypes;
}
ItemInfoViewModelcontain a property namedItemTypes(but in any case do not use the same name for the property your bind to and the name of theSelectList)string ItemTypesit will be bound with the selected option value. But this is awful practice and you should be using a view model with propertiesstringSelectedItemType` andIEnumerable<SelectListItem> ItemTypeListand using the strongly typed@Html.DropDowmListFor(m => m.SelectedItemType, Model.ItemTypeList)Html.LabelFor(model => model.ItemType)you model contains a property namedItemType(which is not the same asItemTypes- plural)