C# ASP.NET mvc DropDownList用于控制器的传递类型
如何将SelectedItem从DropDownList for传递到公共操作结果索引(IndexViewModel IndexViewModel)。提交后,我有C# ASP.NET mvc DropDownList用于控制器的传递类型,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,如何将SelectedItem从DropDownList for传递到公共操作结果索引(IndexViewModel IndexViewModel)。提交后,我有emp=null。 我应该在IndexViewModel.ListOfEmployee=新选择列表(ListOfEmployee,“Number”,“Name”)中放置什么而不是“Number” 控制器: public class HomeController : Controller { // // GET: /Hom
emp=null
。
我应该在IndexViewModel.ListOfEmployee=新选择列表(ListOfEmployee,“Number”,“Name”)中放置什么而不是“Number”
代码>
控制器:
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();
}
}
您应该保留一个简单的值类型属性来存储选定的下拉选项值
public class IndexViewModel
{
public SelectList ListOfEmployee { get; set; }
public int SelectedEmployeeId { get; set; }
}
在你看来
@Html.DropDownListFor(x=>x.SelectedEmployeeId, Model.ListOfEmployee)
在HttpPost操作中,访问此SelectedEmployeeId
属性,该属性将为您提供与所选姓名对应的号码。如果需要完整的employee对象,应使用编号(id)再次查询db
您应该保留一个简单的值类型属性来存储选定的下拉选项值
public class IndexViewModel
{
public SelectList ListOfEmployee { get; set; }
public int SelectedEmployeeId { get; set; }
}
在你看来
@Html.DropDownListFor(x=>x.SelectedEmployeeId, Model.ListOfEmployee)
在HttpPost操作中,访问此SelectedEmployeeId
属性,该属性将为您提供与所选姓名对应的号码。如果需要完整的employee对象,应使用编号(id)再次查询db
无法将
元素绑定到复杂对象(typeofEmployee
)的可能重复项。您的属性emp
必须是typeint
-public-int-emp{get;set;}
可能的重复项无法将
元素绑定到复杂对象(typeofEmployee
)。您的属性emp
必须是类型int
-public intemp{get;set;}
[HttpPost]
public ActionResult Index(IndexViewModel model)
{
var selectedNumber= model.SelectedEmployeeId;
return View();
}