jquery - Submit n forms With one button MVC .NET -


i have page has 2 or more partial views rendered within it. each of views form submittable. supplied models views submitted myproject.models.compositemodel holds object , list of objects (both objects ado.net models, lets call them model_1 , model_2).

the 2+ models rendered partial views have properties same name, , inputs have same name (both in fact model_2 form can have n iterations, , 2 models them selves have properties same names).

i render parent view 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) } 

... , 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 list", "index") 

i need way submit either forms asynchronously via either jquery or .net ajax helper lib. alternatively if can have button takes models divided , edited, , reassembles compositmodel , submits it. in razor syntax or whatever else, 1 method fine, , if there's built in way prefer way rather expanding lib database.

rather dividing them separate forms , recombining them, why not create 1 formatted form submit? welcome magical world of editortemplates!

1. create editortemplate

create editor template in /views/shared/editortemplates/model_x.cshtml or [parent_root]/editortemplates/model_x.cshtml

it 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 editor template in view.

they replace use of partials (which not work forms) , this.

@model myproject.models.compositemodel @{     viewbag.title = "edit";     layout = "~/views/shared/_layout.cshtml"; }  <h2>edit</h2>  @using (html.beginform()) {     @html.editorfor(model => model.table1rowdata)     (int = 0; < model.table2rows.count(); i++)     {         @html.editorfor(m => m.table2rows[i])     }     <p><input type="submit" /></p> } 

3. success!

when post form, should have complete model myproject.models.compositemodel filled in.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -