说我有以下型号:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public class Town
{
public string Name { get; set; }
public IEnumerable<Person> People { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后,在我的Razor视图中,我有这个:
@model Town
@using(Html.BeginForm())
{
<table>
@foreach(var person in Model.People)
{
<tr>
<td>@Html.TextBoxFor(m => person.Name)</td>
<td>@Html.TextBoxFor(m => person.Age)</td>
</tr>
}
<table>
<input type="submit" />
}
Run Code Online (Sandbox Code Playgroud)
然后,我有一个POST的动作,如下所示:
[HttpPost]
public ActionResult Index(Town theTown)
{
//....
}
Run Code Online (Sandbox Code Playgroud)
当我发布时,IEnumerable<Person>没有遇到.如果我在Fiddler中查看它,该集合只发布一次,并且不会枚举该集合,所以我得到:
People.Name = "whatever"
People.Age …Run Code Online (Sandbox Code Playgroud) 在MVC4中:
我的模型中有以下属性用于下拉列表:
public SelectList Subjects { get; set; }
Run Code Online (Sandbox Code Playgroud)
我在页面加载的Index()Action中设置了Subjects属性并返回模型.
使用SelectListItems可以很好地填充下拉列表.
@Html.DropDownListFor(x => x.Subject, new SelectList(Model.Subjects, "Text", "Text", "Other"))
Run Code Online (Sandbox Code Playgroud)
当我提交表单时,模型中的Subjects SelectList已更改为null.必须有一种简单的方法来持久化HttpPost.我想我也想提交和发布这个SelectList,以及所有表单字段?我该怎么做?