说我有以下型号:
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; }
}
然后,在我的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" />
}
然后,我有一个POST的动作,如下所示:
[HttpPost]
public ActionResult Index(Town theTown)
{
//....
}
当我发布时,IEnumerable<Person>没有遇到。如果我在Fiddler中查看它,该集合只发布一次,并且不会枚举该集合,所以我得到:
People.Name = "whatever"
People.Age = 99
但是,如果我将People更改为an 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>
}
有用。难道我做错了什么?我错过了什么?
30秒到达战场
慕容708150