c# - Iteration Through N Number Of Children of a Model Instance - Entity Framework/LINQ -


hope spoke correctly in title. basically, have mvc/entity framework application (code first) , can see there's iteration happening (provided scaffolding) write whatever labels, editor-for, etc. foreach object dynamically created user of application (create.cshtml), etc.

what need not have specify each property name labels, editorfor, etc. need have code each property of object , display id..

i figured have been pretty straight forward. but, in addition neglecting of such use amazing architects of scaffolding option, haven't figured out either. i'm desiring, if possible, use entity framework (or guess querying linq, right?), lambda expressions, in fashion of rest of beautiful code.

p.s. see there're few questions out there related question in way. not close enough asking solve problem..

based on number of views , no answers, perhaps example needed. below class called item has number of properties. each time administrator adds item, new instance created , these properties set. that's fine , dandy, don't want have specify properties are.. want like, foreach(property in item){html.displaynamefor(item.propertyname)} , again, appropriate, foreach(property in item){html.editorfor(item.propertyname)} way label , input box created without me having specify: "item.name, item.category, item.price, item.blah" , if add more properties have go code again , add more stuff...

so, being said, here's class:

public class item {     public int id { get; set; }      [required]     public string itemname { get; set; }      [datatype(datatype.date)]     public datetime availabledate { get; set; }      [required]     public string category { get; set; }      [range(1, 10000000)]     [datatype(datatype.currency)]     public decimal price { get; set; } } 

and here's need dynamic stuff:

<table>     <tr>         <th>             @html.displaynamefor(foreach(modelproperty in model.properties)         {modelproperty.propertyname}         </th>     <tr> </table> 

and

<table>      <tr>         <th>             @html.displayfor(foreach(modelproperty in model.properties)         {modelproperty.propertyname}         </th> </table> 

on item class, can hide specific columns turning off scaffolding:

public class item {     [scaffoldcolumn(false)]     public int id { get; set; } } 

then in view, can this:

@model item  @html.editorformodel() 

this automatically search these views:

  • ~/views/{controller}/editortemplates/item.cshtml
  • ~/views/shared/editortemplates/item.cshtml
  • (a few others .vbhtml too)
  • ~/views/shared/object.cshtml

if none of exist, go default object editor. examines model metadata build form. model metadata built automatically via reflection of properties.

what you're looking for, doing loop on object properties, takes bit more , require reflection. in view, recommend against - logic , views should simple templates.

as example, wrote custom object.cshtml template match how lay out forms. goes in ~/views/shared/editortemplates/object.cshtml. automatically loop on properties (i omitted couple customized extensions)

<dl>     @foreach (var prop in         viewdata.modelmetadata.properties             .where(pm => pm.showforedit && !viewdata.templateinfo.visited(pm)))     {         <dt>@html.label(prop.propertyname)</dt>         <dd>             @html.editor(prop.propertyname)             @html.validationmessage(prop.propertyname)         </dd>     } </dl> 

you'll find basic editor templates won't cover need. basic forms, yes. complex objects, not much.

for reference, here's how you'd loop properties reflection, again, bad , redundant idea this:

@{      // not way, reflection. have check various dataannotations     // each property.     var properties = model.gettype().getproperties();      foreach (var property in properties)     {         @property.name     }       // better way (sort of), loop data built     // in view metadata properties.     foreach (var property in viewdata.modelmetadata.properties)     {         @property.displayname     } } 

you can check here more documentation on viewdata.modelmetadata contains.


Comments