database - ASP.NET MVC 4 How to edit data that comes from a foreign key -


here problem :

i have database contains several attributes name etc. have 2 specifics attributs, iissettings , sqlsettings.

  public class environment   {     public int id { get; set; }      public string name { get; set; }      public virtual sqlsettings sql { get; set; }      public virtual iissettings iis { get; set; } ... } 

iissettings , sqlsettings contains both name , id foreign keys.

when try update environment, , change attributs in iissettings or sqlsettings, visual studio telling me nothing has changed, because in "standards" attributs, nothing has changed. thing changed values inside iissettings or sqlsettings. so, wanted know how save changes database, when want update iissettings or sqlsettings?

my viewmodel :

public class environmentviewmodel : viewmodelbase {     public environment environment { get; set; }     public iissettings iissettings { get; set; }     public sqlsettings sqlsettings { get; set; }      //create/details/delete functions etc.      internal void edit()     {         try         {             db.entry(environment).state = entitystate.modified;             db.savechanges();         }         catch (dbupdateconcurrencyexception ex)         {             debug.writeline("\ndbupdateconcurrencyexception : " + ex);         }     }     ... } 

viewmodelbase db set. standard dbset :

public class viewmodelbase {     protected mydbcontext db = new mydbcontext (); } 

and

public class mydbcontext : dbcontext {     public mydbcontext()         : base()     {     }      public dbset<environment> environments { get; set; } } 

working code :

<div class="editor-label">     @html.labelfor(model => model.environment.name) </div> <div class="editor-field">     @html.editorfor(model => model.environment.name)     @html.validationmessagefor(model => model.environment.name) </div> 

not working code :

<div class="editor-label">     @html.labelfor(model => model.environment.iis.iisserver) </div> <div class="editor-field">     @html.editorfor(model => model.environment.iis.iisserver)     @html.validationmessagefor(model => model.environment.iis.iisserver) </div> 

it's when try save changes database , change name of sqlsettings error. dbupdateconcurrencyexception.

any suggestions? (if forgot please tell me) :)

i found out how it. needed add

@html.hiddenfor(model => model.environment.id) @html.hiddenfor(model => model.environment.iis.iissettingsid) @html.hiddenfor(model => model.environment.sql.sqlsettingsid) 

to edit view. , edit() method inside environmentcontroller :

db.environments.attach(environment); db.entry(environment).state = entitystate.modified; db.entry(environment.iis).state = entitystate.modified; db.entry(environment.sql).state = entitystate.modified; 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -