Asp.net mvc 3 MVC3提交在我的复杂数据类型上返回null
在我的MVC3项目中,我有以下模型:Asp.net mvc 3 MVC3提交在我的复杂数据类型上返回null,asp.net-mvc-3,Asp.net Mvc 3,在我的MVC3项目中,我有以下模型: public class CustomerModules { public int ModuleId { get; set; } public string ModuleName { get; set; } public int CustId { get; set; } public bool IsActive { get; set; } public DateTime? ActiveDate { get; set;
public class CustomerModules
{
public int ModuleId { get; set; }
public string ModuleName { get; set; }
public int CustId { get; set; }
public bool IsActive { get; set; }
public DateTime? ActiveDate { get; set; }
}
public class CustomerModuleList
{
public IEnumerable<CustomerModules> Modules { get; set; }
}
我的视图片段是:
<% using (Html.BeginForm("EditModules","Admin",FormMethod.Post, new { enctype = "multipart/form-data" })){ %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>CustomerModuleList</legend>
<table>
<tr>
<th>Module Name</th>
<th>Active</th>
<th>Active Date</th>
</tr>
<% foreach (var module in Model.Modules){%>
<tr>
<td><%:Html.Label(module.ModuleName) %></td>
<td>
<%CustomerModules module1 = module;%>
<%:Html.CheckBoxFor(x=>module1.IsActive) %>
</td>
<td><%:Html.Label(module.ActiveDate.ToString()) %></td>
</tr>
<% } %>
</table>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
CustomerModuleList
模块名
活跃的
有效日期
模块1.i活动)%>
当我提交回控制器时,
IEnumerable
模块返回为空。我想知道如何在MVC3中提交复杂类型的IEnumerable
?有人知道吗?问题在于您没有指定表单提交的对象的类型为CustomerModuleList
在views/Shared/EditorTemplates文件中,创建名为CustomerModuleList.cshtml和CustomerModules.cshtml的视图
在CustomerModuleList.cshtml中放入
@model CustomerModuleList
<% foreach (var module in Model.Modules){%>
Html.EditorFor(x => module);
<% } %>
使用我制作的不同类型的IEnumerable对此进行了测试,结果成功。请查看此帖子:我希望它能帮助您解决应用程序中的问题:)
PS:该示例也可以在MVC3上运行,无需任何额外的工作。这是一个类似但不相关的问题。我在这里添加它是希望我能为其他一些可怜的开发人员节省4个多小时的故障排除时间。我不知道这是否只是在我的系统上,但是…在MVC 3中,我发现如果模型包含任何类型的列表属性(list、ienumerable、array…)和名称Member或OrgMember,那么模型绑定器将完全失败!想想看。非常感谢,我使用的是MVC4,也有同样的问题。然而,我的列表被称为其他名称,我创建了这个问题,但我发现重命名列表名称有帮助。。。
@model CustomerModuleList
<% foreach (var module in Model.Modules){%>
Html.EditorFor(x => module);
<% } %>
@model CustomerModules
<tr>
<td><%:Html.Label(Model.ModuleName) %></td>
<td>
<%:Html.CheckBoxFor(x=>x.IsActive) %>
</td>
<td><%:Html.Label(Model.ActiveDate.ToString()) %></td>
</tr>
Html.EditorFor(x => Model)