在ASP.NET MVC應用程序中, > >
>>>需要將包含輸入元素的動態數組的形式提交為iEnumerable&lt ; 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中文網其他相關文章!