c# - In EF/MVC, is it possible to edit virtual property? -


so have following 2 model:

[table("company")] public class company {     public virtual list<useraccount> users {         {             // load users here         }     } } [table("useraccount")] public class useraccount {     public string email { get; set; } } 

in view, try edit it:

@model myxsite2013.company <table> foreach (useraccount ua in model.users) {     <tr class="norowhover">         <td>             @html.textboxfor(modelitem => ua.email)         </td>     </tr> } </table> 

and on postback, try save:

public actionresult edit(company companymodel) {     company companycontext = database.companies.find(companymodel.id);     database.entry(companycontext).currentvalues.setvalues(companymodel);     companycontext.isactive = true;     database.savechanges(); } 

this of course, not save changes users, in fact, it's not seeing changes coming in.

your problem write view in wrong way. if want bind model contains list, should set special names them.

something like:

@model myxsite2013.company <table> @{    var i=0; } @foreach (useraccount ua in model.users) {     <tr class="norowhover">         <td>             <input type="text" name="model.users[@i].email"/>         </td>     </tr>     i++; } </table> 

more information on theme here

update

you can use htmlhelper methods:

@using(html.beginform("post", "home", formmethod.post)) {     int = 0;     foreach (var ua in model.users)     {         @html.textbox(string.format("model.users[{0}].email", i), ua.email)         i++;     }     <button type="submit">submit</button> } 

and there should public setter:

[table("company")] public class company {     public virtual list<useraccount> users {         get;         set;     } } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -