说我有以下型号:
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; } }
然后,在我的剃刀视图中,我有这:
@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" /> }
然后,我有一个POST的行动,像这样:
[HttpPost] public ActionResult Index(Town theTown) { //.... }
当我发布,IEnumerable< Person>没有遇到。如果我在Fiddler看看它,该集合只发布一次,并没有枚举集合,所以我得到:
People.Name = "whatever" People.Age = 99
但是,如果我将People更改为IList并使用for循环而不是foreach …
@for(var i = 0;i < Model.People.Count;i++) { <tr> <td>@Html.TextBoxFor(m => Model.People[i].Name)</td> <td>@Html.TextBoxFor(m => Model.People[i].Age)</td> </tr> }
有用。我做错了什么?我缺少什么?