2016-04-11 163 views
0

如何從DropDownListFor傳遞的SelectedItem到public ActionResult Index(IndexViewModel indexViewModel)。提交後,我有emp=null。 我應該代替地方"Number"IndexViewModel.ListOfEmployee = new SelectList(ListOfEmployee, "Number", "Name");ASP.NET MVC DropDownListFor傳遞類型控制器

控制器:

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; } 
} 
+1

的可能的複製[如何獲得的DropDownList的SelectedValue在控制器中MVC4(http://stackoverflow.com/questions/27901175/how-to-get-dropdownlist-selectedvalue-in-controller-in-mvc4) –

+0

你不能將一個'