Submit n forms With one button MVC .NET

I have a page that has 2 or more partial views rendered within it. Each of the views is a form that is submittable. The supplied models for the views are submitted from the MyProject.Models.CompositeModel that holds an object and a list of objects (both objects are ADO.NET Models, Lets call them Model_1 and Model_2).

The 2+ models that are rendered to the partial views have properties with the same name, and the inputs also have the same name (both in the fact that model_2 form can have n iterations, and that the 2 models them selves have some properties with the same names).

I render the parent view as such:

@model MyProject.Models.CompositeModel
@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>

@Html.Partial("_Table1EditRowPartial",Model.Table1RowData)
@foreach (var Table2RowData in Model.Table2Rows) { 
    @Html.Partial("_Table2EditRowPartial",Table2RowData)
}

... and the partial views are:

@model MyProject.Models.Model_x

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.LabelFor(model => model.prop_1)
    @Html.EditorFor(model => model.prop_1)
    @Html.ValidationMessageFor(model => model.prop_1)

    ...

    @Html.LabelFor(model => model.prop_n)
    @Html.EditorFor(model => model.prop_n)
    @Html.ValidationMessageFor(model => model.prop_n)

    <input type="submit" value="Save" />
}

@Html.ActionLink("Back to List", "Index")

I need a way to submit either all forms asynchronously via either jQuery or the .NET ajax helper lib. Alternatively if I can have a button that takes the models divided and edited, and then reassembles it into the CompositModel and submits it. It could be in Razor syntax or whatever else, any one method do fine, and if there's a built in way I would prefer to do it that way rather then expanding my lib database.

Answers


Rather than dividing them into separate forms and recombining them, why not just create 1 well formatted form to submit? Welcome to the magical world of EditorTemplates!

1. Create an EditorTemplate

Create an editor template in /Views/Shared/EditorTemplates/Model_x.cshtml or [parent_root]/EditorTemplates/Model_x.cshtml

It will look something like this.

@model MyProject.Models.Model_x

@Html.LabelFor(model => model.prop_1)
@Html.EditorFor(model => model.prop_1)
@Html.ValidationMessageFor(model => model.prop_1)

...

@Html.LabelFor(model => model.prop_n)
@Html.EditorFor(model => model.prop_n)
@Html.ValidationMessageFor(model => model.prop_n)
2. Use the editor template in your view.

They will replace your use of partials (which do not work well for forms) and will look something like this.

@model MyProject.Models.CompositeModel
@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Edit</h2>

@using (Html.BeginForm())
{
    @Html.EditorFor(model => model.Table1RowData)
    for (int i = 0; i < Model.Table2Rows.Count(); i++)
    {
        @Html.EditorFor(m => m.Table2Rows[i])
    }
    <p><input type="submit" /></p>
}
3. Success!

When you post the form, you should have a complete model MyProject.Models.CompositeModel filled in.


Need Your Help

Listening to Events in the calendar from more than one person using EWS API

c# api events exchangewebservices exchange-server-2010

Simply I would like to receive a notification every time someone added a new appointment or made any changes on what he/she has.

How to programmatically set TextBlock Foreground Hex Color

windows-phone-8

I have to use Foreground Hex color of a Textblock Pragmatically.it does not work.Please help me.