0
我有家庭控制器動作樣結合:如何做下拉列表中mvc4
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
List<SelectListItem> oList = new List<SelectListItem>();
oList.Add(new SelectListItem() { Text = "Rest1", Value = "1" });
oList.Add(new SelectListItem() { Text = "Rest2", Value = "2", Selected=true});
oList.Add(new SelectListItem() { Text = "Rest3", Value = "3" });
Person p = new Person() { PossibleSchools = oList };
return View(p);
}
另一個都會調用上提交按鈕點擊是::
public void ModelBinding(Person p)
{
var name = p.Name;
}
鑑於我有下面的代碼::
<div class="content-wrapper">
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>@ViewBag.Message</h2>
</hgroup>
<form action="Home/ModelBinding" method="post">
<p>
Name :: <input type="text" name="Name"/>
Restaurant :: @Html.DropDownList("PossibleSchools");
<input type="submit" />
</p>
</form>
</div>
而且我的模型是::
public class Person
{
public string Name { get; set; }
public List<SelectListItem> PossibleSchools { get; set; }
}
問題是,每當我嘗試調試這個應用程序,我可以看到Name字段被綁定,但PossibleSchools計數爲零。
因此,dropdownlist沒有綁定。