【发布时间】:2016-12-29 15:15:01
【问题描述】:
我有两个类,第一个包含一个列表。 VIEW 接收一个 List 作为模型。 当我发布视图时,我可以在控制器中检索每个简单的属性。但子模型的 List 始终为空。
两个类:
public class MyModel
{
public int ModelId {get;set;}
public string Description {get;set;}
public List<SubModel> SubModels {get;set;}
}
public class SubModel
{
public int MySubModelId {get;set;}
public string Description {get;set;}
}
在视图页面中收到的模型是
@model List<MyModel>
根据互联网上的建议,我为编辑器创建了一个编辑器,以在视图中呈现我的课程:
@model MyModel
<tr id="@Model.ModelId">
@Html.HiddenFor(m => m.ModelId)
@Html.HiddenFor(m => m.Description)
<td>@Model.Description</td>
</tr>
@for(int i = 0; i < Model.SubModels.Count; i++)
{
// How to render the class here to be able to be post in the controller?
// @Html.HiddenFor(m => m.SubModels[i]) will not work of course...
}
【问题讨论】:
-
请向我们展示您的控制器和此请求管道中的任何其他代码。
-
没有看到控制器很难分辨。但我相信您不能将复杂类型传递回控制器,因为它通常会导致 null。据说这是因为查询字符串可能太长以及其他各种原因
标签: c# asp.net-mvc list http-post model-binding