c# - PropertyInfo to Expression<Func<TModel, TProperty>> -


i want create kind of webgrid 2.0 asp.net , razor.

defining modelclass (tdata) webgrid should create html table on own.

the properties of tdata should read via reflection (typeof(tdata).getproperties). attributes of properties should define css , html styling or data (displaynameattribute => columnheader).

now came point, when want call htmlhelper.displayfor(...propertyinfotoexpression...) render datas content.

how call displayfor, when got data(row)/model , propertyinfo?


webgrid-class:

public class tablemodel<tdata>{       private readonly ilist<tdata> _rows;      private readonly ilist<tablecolumn<tdata>> _columns = new list<tablecolumn<tdata>>();        public tablemodel(ilist<tdata> rows) {         _rows = rows;          propertyinfo[] propertyinfos = typeof(tdata).getproperties();         foreach (propertyinfo property in propertyinfos) {             if (!attribute.isdefined(property, typeof(nocolumnattribute))) {                 _columns.add(new tablecolumn<tdata>(property));             }         }      }      private mvchtmlstring getcellhtml(htmlhelper<tdata> helper, tablecolumn column, tdata datarow){           tagbuilder celltagbuilder = new tagbuilder("td");          celltagbuilder.innerhtml = helper.displayfor(...propertyinfotoexpression...)      }      public mvchtmlstring tohtml(htmlhelper helper){          tagbuilder tabletagbuilder = new tagbuilder("table");          tagbuilder headtagbuilder = new tagbuilder("thead");          tagbuilder bodytagbuilder = new tagbuilder("tbody");           ...          return new mvchtmlstring(tabletagbuilder);     } } 

sample class tdata catch idea:

public class usermodel{        [nocolumnattribute]       public int id{get;set;}        [cssclass("name")]       public string firstname {get;set;}        [cssclass("name")]       public string lastname{get;set;}        [cssclass("mail")]       public string mail{get;set;}        [cssclass("phone")]       public string phone{get;set;}  } 

have tried...

private mvchtmlstring getcellhtml(htmlhelper<tdata> helper, tablecolumn column, tdata datarow){       tagbuilder celltagbuilder = new tagbuilder("td");      celltagbuilder.innerhtml = helper.display(column.propertyinfo.name, datarow);      ... } 

...?

if need displayfor method getcellhtml don't need build expression propertyinfo.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -