c# - ASP.NET MVC. Code First Parent navigational property not populated automatically -


given these poco classes

class mainclass{      [key] int id {get;set;}      public string name {get;set;}      [required]      public virtual icollection<subitem> subitems {get;set} // unique sub items  } 

and

class subitem{      [key] int id {get; set;}      public string name {get;set;}      [required] // required nedded. subitem cannot exist it's self     public mainclass parent {get; set;} } 

when posting , binding controller, set, , set subitems state to:

public virtual actionresult edit([bind()] mainclass entity) {     foreach(var subitem in entity.subitems)         db.entry(subitem).state = system.data.entity.entitystate.modified;      if (modelstate.isvalid) { // fails here         db.entry(entity).state = system.data.entity.entitystate.modified;         db.savechanges();         return redirecttoaction("edit", new { edited = "true" });     }  } 

however modelstate contains "parent cannot null" error each item. , validations fails. reason "parent" property not automatically populated. how can avoid it?

i'm rendering view this.

@model mainclass @html.hiddenfor(m=>m.id) @html.editorfor(m=>m.name) @html.editorfor(m=>m.subitems) 

where subitem has fallowing editortemplate

@model subitem @html.hiddenfor(m=>m.id) @html.editorfor(m=>m.name) 

and [required] parent needed due cascade deleting.

without [required] works good. , parent property added when db saved. cascade deleting turned off on migration.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -