asp.net mvc 3 - Propogate Changes to Nested Entities in MVC 3 -


i have 2 poco classes user , address. address complex object, , user has 1 address. want create single user view allows create/edit of address on same form via mvc scaffolding + entity framework + repository pattern. create user view works correctly, when try make changes address in user edit view, changes don't propagated. how force changes propogated down address object user view? else auto generated.

poco classes

public class testdb : dbcontext {     protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {     }      public dbset<address> addresses { get; set; }     public dbset<user> users { get; set; } }  public class address {     public int id { get; set; }     public string street1 { get; set; }     public string street2 { get; set; }     public string city { get; set; }     public string state { get; set; }     public string postalcode { get; set; }     public string country { get; set; } }  public class user {     public int id { get; set; }     public string username { get; set; }      /*[foreignkey("address")]     public int addressid { get; set; }*/     public virtual address address { get; set; } } 

edit user view

<% using (html.beginform()) { %> <%: html.validationsummary(true) %> <fieldset>     <legend>user</legend>      <%: html.hiddenfor(model => model.id) %>         <%: html.partial("createoredit", model) %>     <legend>address</legend>     <div class="editor-label">         <%: html.labelfor(model => model.address.street1) %>     </div>     <div class="editor-field">         <%: html.editorfor(model => model.address.street1)%>         <%: html.validationmessagefor(model => model.address.street1)%>     </div>      <div class="editor-label">         <%: html.labelfor(model => model.address.street2)%>     </div>     <div class="editor-field">         <%: html.editorfor(model => model.address.street2)%>         <%: html.validationmessagefor(model => model.address.street2)%>     </div>      <div class="editor-label">         <%: html.labelfor(model => model.address.city)%>     </div>     <div class="editor-field">         <%: html.editorfor(model => model.address.city)%>         <%: html.validationmessagefor(model => model.address.city)%>     </div>      <div class="editor-label">         <%: html.labelfor(model => model.address.state)%>     </div>     <div class="editor-field">         <%: html.editorfor(model => model.address.state)%>         <%: html.validationmessagefor(model => model.address.state)%>     </div>      <div class="editor-label">         <%: html.labelfor(model => model.address.postalcode)%>     </div>     <div class="editor-field">         <%: html.editorfor(model => model.address.postalcode)%>         <%: html.validationmessagefor(model => model.address.postalcode)%>     </div>      <div class="editor-label">         <%: html.labelfor(model => model.address.country)%>     </div>     <div class="editor-field">         <%: html.editorfor(model => model.address.country)%>         <%: html.validationmessagefor(model => model.address.country)%>     </div>     <p>         <input type="submit" value="save" />     </p> </fieldset> <% } %>  <div>     <%: html.actionlink("back list", "index") %> </div> 

editor template address:

<%@ control language="c#" inherits="system.web.mvc.viewusercontrol<addresstest.models.address>" %>  <script src="<%: url.content("~/scripts/jquery-1.5.1.min.js") %>" type="text/javascript"></script> <script src="<%: url.content("~/scripts/jquery.validate.min.js") %>" type="text/javascript"></script> <script src="<%: url.content("~/scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>  <% using (html.beginform()) { %> <%: html.validationsummary(true) %> <fieldset>     <legend>address</legend>      <%: html.hiddenfor(model => model.id) %>      <div class="editor-label">         <%: html.labelfor(model => model.street1) %>     </div>     <div class="editor-field">         <%: html.editorfor(model => model.street1) %>         <%: html.validationmessagefor(model => model.street1) %>     </div>      <div class="editor-label">         <%: html.labelfor(model => model.street2) %>     </div>     <div class="editor-field">         <%: html.editorfor(model => model.street2) %>         <%: html.validationmessagefor(model => model.street2) %>     </div>      <div class="editor-label">         <%: html.labelfor(model => model.city) %>     </div>     <div class="editor-field">         <%: html.editorfor(model => model.city) %>         <%: html.validationmessagefor(model => model.city) %>     </div>      <div class="editor-label">         <%: html.labelfor(model => model.state) %>     </div>     <div class="editor-field">         <%: html.editorfor(model => model.state) %>         <%: html.validationmessagefor(model => model.state) %>     </div>      <div class="editor-label">         <%: html.labelfor(model => model.postalcode) %>     </div>     <div class="editor-field">         <%: html.editorfor(model => model.postalcode) %>         <%: html.validationmessagefor(model => model.postalcode) %>     </div>      <div class="editor-label">         <%: html.labelfor(model => model.country) %>     </div>     <div class="editor-field">         <%: html.editorfor(model => model.country) %>         <%: html.validationmessagefor(model => model.country) %>     </div>      <p>         <input type="submit" value="save" />     </p> </fieldset> <% } %>  <div> <%: html.actionlink("back list", "index") %> </div> 

change user edit view:

<% using (html.beginform()) { %> <%: html.validationsummary(true) %> <fieldset>     <legend>user</legend>      <%: html.hiddenfor(model => model.id) %>         <%: html.partial("createoredit", model) %>     <%: html.editor("address", model.address) %>      <p>         <input type="submit" value="save" />     </p> </fieldset> <% } %> 

the solution modify user update repository method , mark entity state modified address. needed add hidden property address.id on user edit view.

user insert/update repository method

    public void insertorupdate(user user)     {         if (user.id == default(int)) {             // new entity             context.users.add(user);         } else {             // existing entity             context.entry(user.address).state = entitystate.modified; // update address             context.entry(user).state = entitystate.modified;         }     } 

additional hidden property on user edit view

<%: html.hiddenfor(model => model.id) %> <%: html.hiddenfor(model => model.address.id) %> <!-- added line! --> 

controller edit action

    // post: /user/edit/5      [httppost]     public actionresult edit(user user)     {         if (modelstate.isvalid) {             userrepository.insertorupdate(user);             userrepository.save();             return redirecttoaction("index");         } else {             viewbag.possibleaddresses = addressrepository.all;             return view();         }     } 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -