I having below code for different artifacts,
Entity
public class ChooseFirst
{
public int ChooseFirstId { get; set; }
public string ChooseFirstName { get; set; }
}
View Model
public class SelectViewModel
{
public IEnumerable<SelectListItem> ListChooseFirst { get; set; }
}
Controller/Get Action
//
// GET: /MenuOne/
public ActionResult MenuOne()
{
var selectViewModel = new SelectViewModel
{
ListChooseFirst = ChooseFirstList()
};
return View(selectViewModel);
}
private IEnumerable<SelectListItem> ChooseFirstList()
{
//here data comes from database
List<ChooseFirst> list = _getFComboService.GetFComboList();
List<SelectListItem> items = new List<SelectListItem>();
foreach (ChooseFirst chooseFirst in list)
{
SelectListItem item = new SelectListItem();
item.Text = chooseFirst.ChooseFirstName;
item.Value = chooseFirst.ChooseFirstId.ToString();
items.Add(item);
}
return items;
}
HTML View
@Html.DropDownList("FCombo", Model.ListChooseFirst, "--Select One--")
Now Problem when I post my form the value for “selectViewModel” is NULL, Is there a need to model binder, please suggest and help me how to create Model Binder for this?
//
// POST: /MenuOne/
[HttpPost]
public ActionResult MenuOne(SelectViewModel selectViewModel)
{
return View();
}
that is correct. all that is posted to the server is ‘FCombo’ with a value of the selected option. there is no enumeration of select list items. instead what you need is a view model for the posted data.
or you can simply pass the parameter directly as an argument