c# - MVC 4 Model binding a nested model that is used in partial views -


i got following problem, when trying bind model in controller method, nested model not bound (input name's not match, because it's used in partial view).

let me illustrate problem code samples:

controller:

public class testcontroller : controller {     public actionresult create()     {         var model = new test2();         model.basisgegevens.name = "test";         model.basisgegevens.omschrijving = "omschrijving";          return view(model);     }      [httppost]     public actionresult create(test2 model)     {         return view(model);     } } 

model:

public class test {     public string name { get; set; }      public string omschrijving { get; set; } }  public class test2 {     public test2()     {         this.basisgegevens = new test();     }      public int periodevanid { get; set; }      public int periodetotid { get; set; }      public test basisgegevens { get; set; } } 

view:

@model webapplication4.models.test2  @using (html.beginform())  {     @html.antiforgerytoken()      <div class="form-horizontal">         <h4>test2</h4>         <hr />         @html.validationsummary(true)          <div class="form-group">             @html.labelfor(model => model.periodevanid, new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.periodevanid)                 @html.validationmessagefor(model => model.periodevanid)             </div>         </div>          <div class="form-group">             @html.labelfor(model => model.periodetotid, new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.periodetotid)                 @html.validationmessagefor(model => model.periodetotid)             </div>         </div>          @html.partial("~/views/test/partials/naam.cshtml", model.basisgegevens)          <div class="form-group">             <div class="col-md-offset-2 col-md-10">                 <input type="submit" value="create" class="btn btn-default" />             </div>         </div>     </div> } 

partial view:

@model webapplication4.models.test  <div class="form-group">     @html.labelfor(model => model.name, new { @class = "control-label col-md-2" })     <div class="col-md-10">         @html.editorfor(model => model.name)         @html.validationmessagefor(model => model.name)     </div> </div>  <div class="form-group">     @html.labelfor(model => model.omschrijving, new { @class = "control-label col-md-2" })     <div class="col-md-10">         @html.editorfor(model => model.omschrijving)         @html.validationmessagefor(model => model.omschrijving)     </div> </div> 

the model 'test' used in partial view used controller, therefore cannot changed input field names (to allow binding).

this sent server: periodevanid:0 periodetotid:0 name:test omschrijving:omschrijving

i want bottom 2 properties (from nested model), renamed in model binding @ controller level to: basisgegevens.name basisgegevens.omschrijving

that allow binding, , model validation kick in properly.

does know solution simple model binding problem?

you need render partial editor. use following

@html.editorfor(model => model.basisgegevens, "~/views/test/partials/naam.cshtml") 

this name inputs correctly should bind. tells razor partial part of form not unrelated content


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -