Home > Backend Development > C++ > How to Properly POST Form Arrays from ASP.NET MVC to an IEnumerable Model?

How to Properly POST Form Arrays from ASP.NET MVC to an IEnumerable Model?

Linda Hamilton
Release: 2025-02-02 04:01:09
Original
300 people have browsed it

How to Properly POST Form Arrays from ASP.NET MVC to an IEnumerable Model?

POSTing Form Arrays from ASP.NET MVC

Problem:

In an ASP.NET MVC application, an HTML form containing a dynamic array of input elements needs to be submitted as an IEnumerable model property. However, the action method fails to bind the model with data from the form when using FormCollection as the method parameter.

Solution:

Instead of using a FormCollection, the action method must accept an instance of the correct model type, ConnectBatchProductViewModel, as its parameter. To generate a form that posts form values to an IEnumerable property in the model, follow these steps:

  • Use a for loop to generate the controls for each element in the collection. Each control must be named with indexers to correctly bind to the model.
  • Add a hidden input element to each dynamically added row to allow for deleting items from the view.
  • Update the action method to receive the model as a parameter.

Example Code:

Form Code:

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

Action Method:

public ActionResult Save(ConnectBatchProductViewModel model)
{
  // Model binding will populate the BatchProducts property with data from the form.
  ....
}
Copy after login

Dynamically Adding and Removing Items:

To enable dynamic adding and removing of items, use the BeginCollectionItem helper or an HTML template. The template for dynamically adding new items would be:

<div>
Copy after login

And the script to add a new BatchProducts item would be:

$("#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());
});
Copy after login

The above is the detailed content of How to Properly POST Form Arrays from ASP.NET MVC to an IEnumerable Model?. 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