在我的 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 CustomerModuleList
{
public IEnumerable<CustomerModules> Modules { get; set; }
}
我的控制器如下:
[HttpGet]
public ActionResult EditModules(int custNo)
{
var model = new CustomerModuleList
{
Modules = _customerModules.LoadModulesByCustomerId(custNo)
};
return View(model);
}
[HttpPost]
public ActionResult EditModules(CustomerModuleList model)
{
if (ModelState.IsValid)
{
var custId = model.Modules.First().CustId;
_customerModules.UpdateCustomerModules(model.Modules, custId);
}
return View(model);
}
我的视图片段是:
<% 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>
<% } %>
当我提交回控制器时IEnumerable<CustomerModules>
模块返回为空。我想知道您如何提交IEnumerable
MVC3 中的复杂类型?有人有想法吗?
问题是您没有指定表单提交的对象是 CustomerModuleList 类型。
在您的views/Shared/EditorTemplates 文件中创建名为CustomerModuleList.cshtml 和CustomerModules.cshtml 的视图。
在 CustomerModuleList.cshtml 中放入
@model CustomerModuleList
<% foreach (var module in Model.Modules){%>
Html.EditorFor(x => module);
<% } %>
然后在CustomerModules.cshtml中粘贴
@model CustomerModules
<tr>
<td><%:Html.Label(Model.ModuleName) %></td>
<td>
<%:Html.CheckBoxFor(x=>x.IsActive) %>
</td>
<td><%:Html.Label(Model.ActiveDate.ToString()) %></td>
</tr>
然后在您的视图片段中将 for 循环替换为
Html.EditorFor(x => Model)
使用我制作的不同类型的 IEnumerable 对此进行了测试,并且它有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)