我使用MVC中的dropdownlistfor的HtmlHelper有问题。 当回传时没有什么选择,并在模型列表,选择的项目中的值都为空。
这里是我的模型:
namespace MvcTestWebApp.Models
{
public class Customer
{
public string name { get; set; }
public List<SelectListItem> members { get; set; }
public Member selected { get; set; }
}
public class Member
{
public string name { get; set; }
}
}
控制器:
namespace MvcTestWebApp.Models
{
public class CustomerController : Controller
{
//
// GET: /Customer/
public ActionResult Index()
{
Customer cust = new Customer() { name = "Cust1" };
cust.members = new List<SelectListItem>();
cust.members.Add(new SelectListItem(){Text = "Bob"} );
cust.members.Add(new SelectListItem(){Text = "Dave"} );
return View(cust);
}
[HttpPost]
public ActionResult Index(Customer customer)
{
return View();
}
}
}
并查看:
@model MvcTestWebApp.Models.Customer
@{
ViewBag.Title = "Customer";
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title> Index </title>
</head>
<body>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Customer Data</legend>
@Html.HiddenFor(model => model.name)
@Html.DropDownListFor(model => model.selected, Model.members, "--Select--")
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
</body>
</html>
当我从选择列表中的东西并点击提交返回按钮空值:
任何人都可以阐明我做错了一些轻?