title: PropertyInfo to Expression<Func<TModel, TProperty>>
body: <p>I want to create a kind of WebGrid 2.0 for ASP.NET and Razor.</p>
  
  <p>Defining a ModelClass (TData) the WebGrid should create the HTML for the table on its own.</p>
  
  <p>The Properties of TData should be read via reflection (typeof(TData).GetProperties). The attributes of the properties should define some css and html styling or even some data (DisplayNameAttribute => ColumnHeader).</p>
  
  <p>Now I came to the point, when I want to call htmlHelper.DisplayFor(...propertyInfoToExpression...) to render the datas content.</p>
  
  <p>How could I call DisplayFor, when I only got the data(row)/model and the propertyInfo?</p>
  
  <hr>
  
  <p>WebGrid-Class:</p>
  
  <pre><code>public class TableModel&lt;TData&gt;{
  
       private readonly IList&lt;TData&gt; _rows;
       private readonly IList&lt;TableColumn&lt;TData&gt;&gt; _columns = new List&lt;TableColumn&lt;TData&gt;&gt;();
  
  
       public TableModel(IList&lt;TData&gt; rows) {
          _rows = rows;
  
          PropertyInfo[] propertyInfos = typeof(TData).GetProperties();
          foreach (PropertyInfo property in propertyInfos) {
              if (!Attribute.IsDefined(property, typeof(NoColumnAttribute))) {
                  _columns.Add(new TableColumn&lt;TData&gt;(property));
              }
          }
  
      }
  
      private MvcHtmlString GetCellHtml(HtmlHelper&lt;TData&gt; 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);
      }
  }
  </code></pre>
  
  <p>Sample Class for TData just to catch the idea:</p>
  
  <pre><code>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;}
  
  }
  </code></pre>