How to pass SelectedItem from DropDownListFor to public ActionResult Index(IndexViewModel indexViewModel). After submit i have emp=null.
What i should place instead of "Number" in IndexViewModel.ListOfEmployee = new SelectList(ListOfEmployee, "Number", "Name");
Controller:
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
List<Employee> ListOfEmployee = new List<Employee>();
ListOfEmployee.Add(new Employee { Name = "Jhon", Number = 1 });
IndexViewModel IndexViewModel = new IndexViewModel();
IndexViewModel.ListOfEmployee = new SelectList(ListOfEmployee, "Number", "Name");
return View(IndexViewModel);
}
[HttpPost]
public ActionResult Index(IndexViewModel indexViewModel)
{
return View();
}
}
View:
@model MvcApplication3.Models.IndexViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using(@Html.BeginForm())
{
@Html.DropDownListFor(model=>model.emp ,Model.ListOfEmployee)
<input type="submit" />
}
Model:
public class IndexViewModel
{
public SelectList ListOfEmployee { get; set; }
public Employee emp { get; set; }
}
<select>element to a complex obect (typeofEmployee). You propertyempneed to be typeint-public int emp { get; set; }