-1
出于某种原因,我试图手动绑定复杂模型的数组,但我在控制器中得到空引用异常,活页夹不检索参数。我发现this question这是非常相似的(唯一的区别实际上是模型的类型),但我不明白为什么它不起作用在我的情况。手动绑定复杂模型阵列到控制器动作
这是我的。
在我的控制器
public ActionResult MyAction(ComplexType[] models)
{
foreach(ComplexType c in models) // Exception at this point, the parameter is null
{
//Do stuff
}
}
在我看来:
@using(Html.BeginForm("MyAction","Controller"))
{
@Html.AntiForgeryToken()
<table>
@for (int i = 0; i < Model._MyProperty.Count; i++)
{
var d = Model._MyProperty[i];
@:<tr>
@:<td> @Html.CheckBox("models.Property1["+i+"]", d.Property1)
@Html.Hidden("models.ID["+i+"]",d.ID)
@Html.Hidden("models.Property2["+i+"]")
@Html.Hidden("models.Property3["+i+"]")
@Html.Hidden("models.Property4["+i+"]")
@: </td>
@:</tr>
}
//Submit Button
</table>
}
这里是我的课:
class ComplexType
{
int ID {get;set;}
int Property1{get;set;}
int Property2{get;set;}
int Property3{get;set;}
int Property4{get;set;}
}
我试图在改变的ComplexType []到ICollection的控制器,但它没有工作。此外,只是为了获取信息,我的ViewModel不是我要绑定的模型。我需要在这个单一视图中有多种形式处理不同的数据类型。