如果这个问题重复,请原谅我,因为我在 Stack Overflow 上的其他地方找不到这个问题的答案。
我在 ASP.NET 中有一个具有多个字段的表单,并且可以通过 jQuery 添加更多字段。在我的形式中,我想要一群 friend 。
这是 friend 类:
public class Friend {
public string Name { get; set; }
public int Age { get; set; }
}
这是我的表单,用户可以通过单击添加包含这两个字段的行的按钮来添加任意数量的 friend 。例如:
<table>
<tr>
<input type="text" name="friend[0].Name" />
<input type="text" name="friend[0].Age" />
</tr>
<tr>
<input type="text" name="friend[1].Name" />
<input type="text" name="friend[1].Age" />
</tr>
<tr>
<input type="text" name="friend[2].Name" />
<input type="text" name="friend[2].Age" />
</tr>
...
</table>
我如何在 Controller 的函数中将这些作为 friend 列表访问?
最佳答案
我想你已经快到了,
<form method="post" action="/Home/UpdateFriends">
<input type="text" name="[0].Name" value="Curious George" />
<input type="text" name="[0].Age" value="20" />
<input type="text" name="[1].Name" value="Code Complete" />
<input type="text" name="[1].Age" value="50" />
<input type="submit" />
</form>
Controller 应该是这样的
//Action method on HomeController
public ActionResult UpdateFriends(ICollection<Friend> friends) {
//your code here
}
this article更深入地解释它。
关于c# - 如何从 ASP.NET 中的表单访问字段数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36812030/