Multiple forms on one MVC form, created with a loop, only the first submits data

tallpaul

I have the following code, only the first form submits anything, the following submit null values, each model has data. If I change it to just one large form, everything submits. Why do the other individual forms post null values?

View

@model myModel[]
<ul>
    @for (int i = 0; i < Model.Length; i++)
    {
        using (Html.BeginForm("controllerAction", "Controller", FormMethod.Post,
                               new { id="Form"+i }))
        {
            <li>
                @Html.TextBoxFor(a => a[i].property1)
                @Html.CheckBoxFor(a => a[i].property2)
                @Html.HiddenFor(a => a[i].property3)
                <input type="submit" />
            </li>
        }
    }
</ul>

Controller

[HttpPost]
public ActionResult controllerAction(myModel[] models)
{
    ...do stuff...
}
user3559349

The reason is that your creating form controls with indexers in your for loop, and your POST method parameter is myModel[] models.

By default, the DefaultModelBinder requires collection to be zero based and consecutive, so if you attempt to submit the second form, your posting back [1].property1: someValue etc. Because the indexer starts at 1, binding fails and the model is null.

You can solve this by adding a hidden input for an Index property used by the model binder to match up non consecutive indexers

<li>
    @Html.TextBoxFor(a => a[i].property1)
    @Html.CheckBoxFor(a => a[i].property2)
    @Html.HiddenFor(a => a[i].property3)
    <input type="hidden" name="Index" value="@i" /> // add this
    <input type="submit" />
</li>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQuery submits only one form when multiple forms are submitted

From Dev

WTForms only submits first form

From Dev

Button adds input to form, but only first one. Not on multiple forms/divs

From Dev

Javascript Only the first button submits to Ajax form despite different Ids

From Dev

MVC View with a form inside a For Loop submits empty model

From Dev

File upload ASP.NET MVC in multiple submits form

From Dev

Asp.Net multiple submit buttons, enter submits only the first

From Dev

JQuery and multiple inputs,forms,submits

From Dev

Multiple forms in same page with check boxes in that only one form is working remaining forms not working

From Dev

Symfony form submits wrong data

From Dev

Multiple forms in one page MVC 5

From Dev

Multiple forms in one page MVC 5

From Dev

mysql/php loop with multiple forms or just one?

From Dev

MVC form submits after failing validation

From Dev

HTML/Rails form only submits on reload

From Dev

Button click submits the form only on Firefox

From Dev

how to handle multiple form submits in django

From Dev

Ajax request only submits last one

From Dev

Send multiple forms data on one page with ajax

From Dev

Multiple iFrames, only the first one loads

From Dev

multiple if but only runs the first one? javascript

From Dev

Posting only select values from form created with a while loop

From Dev

HTML Form Submits, Data Not Showing in Database

From Dev

Multiple form submit with only one jQuery Ajax

From Dev

Kendo MVC cancel form submission - form always submits

From Dev

ZF2 Form getData with multiple fieldsets only returns data for one fieldset

From Dev

Taking data from multiple forms in a loop and insert into database in a loop

From Dev

Insert multiple forms into same table - only last form is inserted

From Dev

Combine multiple javascript forms into one single form with select options

Related Related

  1. 1

    jQuery submits only one form when multiple forms are submitted

  2. 2

    WTForms only submits first form

  3. 3

    Button adds input to form, but only first one. Not on multiple forms/divs

  4. 4

    Javascript Only the first button submits to Ajax form despite different Ids

  5. 5

    MVC View with a form inside a For Loop submits empty model

  6. 6

    File upload ASP.NET MVC in multiple submits form

  7. 7

    Asp.Net multiple submit buttons, enter submits only the first

  8. 8

    JQuery and multiple inputs,forms,submits

  9. 9

    Multiple forms in same page with check boxes in that only one form is working remaining forms not working

  10. 10

    Symfony form submits wrong data

  11. 11

    Multiple forms in one page MVC 5

  12. 12

    Multiple forms in one page MVC 5

  13. 13

    mysql/php loop with multiple forms or just one?

  14. 14

    MVC form submits after failing validation

  15. 15

    HTML/Rails form only submits on reload

  16. 16

    Button click submits the form only on Firefox

  17. 17

    how to handle multiple form submits in django

  18. 18

    Ajax request only submits last one

  19. 19

    Send multiple forms data on one page with ajax

  20. 20

    Multiple iFrames, only the first one loads

  21. 21

    multiple if but only runs the first one? javascript

  22. 22

    Posting only select values from form created with a while loop

  23. 23

    HTML Form Submits, Data Not Showing in Database

  24. 24

    Multiple form submit with only one jQuery Ajax

  25. 25

    Kendo MVC cancel form submission - form always submits

  26. 26

    ZF2 Form getData with multiple fieldsets only returns data for one fieldset

  27. 27

    Taking data from multiple forms in a loop and insert into database in a loop

  28. 28

    Insert multiple forms into same table - only last form is inserted

  29. 29

    Combine multiple javascript forms into one single form with select options

HotTag

Archive