我在 MVC 中使用 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>
当我从选择列表中选择某些内容并单击提交按钮时,将返回空值:
谁能阐明我做错了什么?