Home > Backend Development > C++ > Why is my ASP.NET MVC form array property null after submission?

Why is my ASP.NET MVC form array property null after submission?

Linda Hamilton
Release: 2025-02-02 04:08:14
Original
602 people have browsed it

Why is my ASP.NET MVC form array property null after submission?

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>
Copy after login

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>
Copy after login
auxiliary method or HTML template. For example, you can create a template for adding new projects:

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template