在ASP.NET MVC应用程序中,
>>>需要将包含输入元素的动态数组的形式提交为iEnumerable< batchProductViewModel>模型属性。但是,当使用formCollection用作方法参数时,操作方法无法用form的数据绑定模型。
解决方案:
>>在每个动态添加的行中添加一个隐藏的输入元素,以允许从视图删除项目。
>示例代码:
@for(int i = 0; i < Model.BatchProducts.Count; i++) { <tr> <td>@Html.TextBoxFor(m => m.BatchProducts[i].Quantity)</td> <td>@Html.TextBoxFor(m => m.BatchProducts[i].BatchName)</td> <td> <input type="hidden" name="BatchProducts.Index" value="@i" /> <a class="deleteRow"></a> </td> </tr> }
表单代码:
public ActionResult Save(ConnectBatchProductViewModel model) { // Model binding will populate the BatchProducts property with data from the form. .... }
动作方法:
<div>
$("#addrow").click(function() { var index = (new Date()).getTime(); // unique indexer var clone = $('#NewBatchProduct').clone(); // clone the BatchProducts item // Update the index of the clone clone.html($(clone).html().replace(/\[#\]/g, '[' + index + ']')); clone.html($(clone).html().replace(/"%"/g, '"' + index + '"')); $("table.order-list").append(clone.html()); });
以上是如何正确发布从ASP.NET MVC到IEnumerable模型的表单阵列?的详细内容。更多信息请关注PHP中文网其他相关文章!