Solve the problem of submitting the ASP.NET MVC table array
In ASP.NET MVC, submitting a form containing the object array may encounter problems. For example:
Your form contains multiple lines, each line represents one project in the array. You use jQuery to dynamically add and delete lines. However, after submitting the form, the array attributes in the model are empty.
Solution:
To solve this problem, you need to manually generate the control of the array in the for loop. This ensures the correct name of the control of the control.
Modified code:
In the view: In the controller:
Other precautions:
<code class="language-csharp">@using (Html.BeginForm("Save", "ConnectBatchProduct", FormMethod.Post)) { .... <table> .... @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> <!-- 包含隐藏输入以跟踪项目索引 --> <a></code>
If you want to add and delete the items in the view, you can use the
<code class="language-csharp">public ActionResult Save(ConnectBatchProductViewModel model) { .... }</code>
Then you can use JavaScript to dynamically add the new project to the form:
The above is the detailed content of Why is my ASP.NET MVC form array property null after submission?. For more information, please follow other related articles on the PHP Chinese website!