我有一个包含多个字段的数据类:
public class Person
{
public int id { get; set }
public string Name { get; set; }
public double Rate { get; set; }
public int Type { get; set; }
}
如果我明白Scott Hanselman's take on binding arrays of objects ,我应该能够创建一个表单 View 来呈现如下所示的 HTML:
<input name="Person[0].id" value="26" type="hidden" />
<input name="Person[0].Name" value="Tom Smith" type="text" />
<input name="Person[0].Rate" value="40.0" type="text" />
<select name="Person[0].Type">
<option selected="selected" value="1">Full Time</option>
<option value="2">Part Time</option>
</select>
<input name="Person[1].id" value="33" type="hidden" />
<input name="Person[1].Name" value="Fred Jones" type="text" />
<input name="Person[1].Rate" value="45.0" type="text" />
<select name="Person[1].Type">
<option value="1">Full Time</option>
<option selected="selected" value="2">Part Time</option>
</select>
然后我应该能够使用如下所示的操作方法在我的 Controller 中捕获此数据:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult People(Person[] array)
{
// Do stuff with array
}
但它不起作用。数组变量始终为空。我将此解释为数据绑定(bind)不起作用。但是为什么?
最佳答案
您的字段应命名为 array[0].id, array[0].Type, ...
它们应该具有数组实例的名称,而不是数组中类型的名称。
或者,您可以将 actioncontroller 的签名更改为: 人[]人
你明白了:-)
关于c# - 绑定(bind)到 ASP.NET MVC 中的强类型对象集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/966923/