entity framework - MVC4 EditorForModel template for EntityFramework -


in mvc4 web application using razor engine , entity framework, possible create template use html helper @html.editorformodel , entities links other tables better displayed.

the example working dbcontext containing 2 dbsets, regions , schools. there many regions, , school may belong 1 region. ideally editor schools show dropdown of regions select from. make template generic enough can call @html.editorformodel helper , form generated in 1 go, , make changes region or schools tables later on , changes reflected in edit form without me needing make alterations.

some code:

public class mycontext : dbcontext {     public mycontext ()         : base("defaultconnection")     {     }      public dbset<region> regions { get; set; }     public dbset<school> schools { get; set; }      [table("regions")]     public class region     {         public region()         {             schools = new list<school>();         }          [key]         public int regionid { get; set; }         [stringlength(256)]         public string regionname { get; set; }          public icollection<school> schools { get; set; }     }      [table("schools")]     public class school     {         public school() { }          [key]         public int schoolid { get; set; }         [stringlength(256)]         public string schoolname { get; set; }         [foreignkey("region")]         public int regionid { get; set; }          public virtual region region { get; set; }     } } 

i have created partial view create display editor form, idea can pass in either region or school view model.

@model object @using (html.beginform()) {                @html.validationsummary("broken stuff:")     @html.editorformodel()                } 

i don't know if possible, new template loop on properties of entity , detect if there linked table (e.g. school contains region) , display list of regions in dropdown.

off corse can. @ here see how.

an alternation use scaffolding , let create views. of times(!) creates correct dropdowns , customize view if want.

and in few situations yo see strange ddls, need correct valuename , dataname parameters in related action methods whic return list items view...


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 -