Как передать SelectedItem из DropDownListFor в public ActionResult Index(IndexViewModel indexViewModel)
. После подачи у меня есть emp=null
. Что я должен поставить вместо "Number"
в IndexViewModel.ListOfEmployee = new SelectList(ListOfEmployee, "Number", "Name");
ASP.NET mvc DropDownListFor pass type to 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();
}
}
Вид:
@model MvcApplication3.Models.IndexViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using(@Html.BeginForm())
{
@Html.DropDownListFor(model=>model.emp ,Model.ListOfEmployee)
<input type="submit" />
}
Модель:
public class IndexViewModel
{
public SelectList ListOfEmployee { get; set; }
public Employee emp { get; set; }
}
Возможный дубликат [Как получить DropDownList SelectedValue в контроллере в MVC4] (http://stackoverflow.com/questions/27901175/how-to-get-dropdownlist-selectedvalue-in-controller-in-mvc4) –
Вы не можете привязать элемент '