0

I have following code, it iterates in for loop through parent list, fills data in one of the field in form,then it iterates in for loop through child list and fills up data in rest of the fields but only first form submits data, all other forms submit null value on submit. Why do the other individual forms post null values? I have ensured that the indexing of binding the model is correct. There is no duplication in element name as the model is bind properly.

@model myModel[]
<ul>
    // For loop through parent list that binds value in parent element
    @for (int i = 0; i < Model.List.Count(); i++)
    {
        // Dynamically created form in for loop
        using (Html.BeginForm("controllerAction", "Controller", FormMethod.Post, new { id="Form"+i })) {
            // Binds the parent property to view.
            <li>
                @Model.List[i].Property1
            </li>
            // For loop through child list that binds value to child element
            @for (int j = 0; j < Model.List[i].ChildList.Count(); j++)
            {
                <li>
                    @Html.EditorFor(model => model.List[i].ChildList[j].Property1)  
                </li>
            }
        }
        // Submit button in each of the form created in for loop
        <input type="submit" />
    }
</ul>

Why only first form data is submitted?

  • Include `` in you view (immediately after the `Html.BeginForm`). Is there any reason why you are giving the forms an `id` attribute? –  Sep 18 '15 at 07:50
  • Your solution solved my problem :) Thanks a ton Stephen. And there is no specific reason for providing forms with an id attribute, I will remove id attribute as it is not required. – rushil bhuptani Sep 18 '15 at 08:00

0 Answers0