ASP.NET MVC局部视图循环Availability对象提交后参数为null的解决问询
解决ASP.NET MVC局部视图提交集合时参数为null的问题
你猜的完全正确!问题根源就是输入框的name属性格式不对。ASP.NET MVC的模型绑定器需要集合类型的表单字段遵循带索引的命名规则(比如[0].Name、[1].Name),才能把表单值正确映射到IEnumerable<Availability>参数上。而你用foreach循环生成的输入框,name都是item.Name这种重复的格式,模型绑定器根本无法识别这是一个集合,所以参数aaa才会返回null。
下面给你两种靠谱的解决方案:
方案一:用for循环替代foreach(简单直接)
因为你的视图模型是List<Availability>(实现了IList<T>,支持索引访问),可以直接用for循环来生成带正确索引的输入框:
修改局部视图_Availability.cshtml的代码:
@model List<WebApplication1.Class.Availability> <p> @Html.ActionLink("Create New", "Create") </p> @using (Html.BeginForm("ListOfPost", "Home", FormMethod.Post)) { <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model[0].Name) </th> <th> @Html.DisplayNameFor(model => model[0].Address) </th> <th> @Html.DisplayNameFor(model => model[0].Gender) </th> <th> @Html.DisplayNameFor(model => model[0].Adult) </th> <th> @Html.DisplayNameFor(model => model[0].Child) </th> </tr> @for (int i = 0; i < Model.Count; i++) { <tr> <td> @Html.EditorFor(model => model[i].Name, new { @class = "Name" }) </td> <td> @Html.EditorFor(model => model[i].Address, new { @class = "Address" }) </td> <td> @Html.EditorFor(model => model[i].Gender, new { @class = "Gender" }) </td> <td> @Html.EditorFor(model => model[i].Adult, new { @class = "Adult" }) </td> <td> @Html.EditorFor(model => model[i].Child, new { @class = "Child" }) </td> </tr> } </table> <input id="btnSubmit" class="btn btn-default" type="submit" value="Submit"> }
这样生成的输入框name会自动变成[0].Name、[1].Name这种格式,模型绑定器就能正确识别并把表单值绑定到aaa参数上了。
方案二:使用EditorTemplate(更优雅,适合复杂场景)
如果你的实体类字段可能变化,或者需要复用这个渲染逻辑,推荐用EditorTemplate的方式:
- 创建EditorTemplate视图
在Views/Shared/EditorTemplates文件夹下(没有就新建),创建名为Availability.cshtml的视图(名字必须和实体类名完全一致):
@model WebApplication1.Class.Availability <tr> <td> @Html.EditorFor(model => model.Name, new { @class = "Name" }) </td> <td> @Html.EditorFor(model => model.Address, new { @class = "Address" }) </td> <td> @Html.EditorFor(model => model.Gender, new { @class = "Gender" }) </td> <td> @Html.EditorFor(model => model.Adult, new { @class = "Adult" }) </td> <td> @Html.EditorFor(model => model.Child, new { @class = "Child" }) </td> </tr>
- 修改局部视图调用EditorTemplate
更新_Availability.cshtml,用Html.EditorForModel()自动渲染整个集合:
@model IEnumerable<WebApplication1.Class.Availability> <p> @Html.ActionLink("Create New", "Create") </p> @using (Html.BeginForm("ListOfPost", "Home", FormMethod.Post)) { <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.First().Name) </th> <th> @Html.DisplayNameFor(model => model.First().Address) </th> <th> @Html.DisplayNameFor(model => model.First().Gender) </th> <th> @Html.DisplayNameFor(model => model.First().Adult) </th> <th> @Html.DisplayNameFor(model => model.First().Child) </th> </tr> @Html.EditorForModel() </table> <input id="btnSubmit" class="btn btn-default" type="submit" value="Submit"> }
这种方式会自动为每个集合项生成带正确索引的name属性,代码更简洁,后续维护实体类字段时也只需要修改EditorTemplate即可。
最后提醒一下:你的ListOfPost方法参数IEnumerable<WebApplication1.Class.Availability> aaa是完全没问题的,只要表单字段格式正确,提交后就能正常获取到用户输入的所有Availability对象了。
内容的提问来源于stack exchange,提问作者CodesDDecodes
相关产品推荐
相关产品推荐

