我想为WebGrid和Razor创建一种ASP.NET 2.0。
定义一个ModelClass (TData) WebGrid应该自己为表创建WebGrid。
TData的属性应该通过反射( The of (TData).GetProperties)读取。属性的属性应该定义一些css和html样式,甚至一些数据(DisplayNameAttribute => ColumnHeader)。
现在我想说的是,当我想打电话给htmlHelper.DisplayFor(...propertyInfoToExpression...)时若要呈现数据内容,请执行以下操作。
当我只得到数据(行)/model和propertyInfo时,我怎么能调用propertyInfo?
WebGrid-等级:
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);
}
}TData的样例类就是为了抓住这个想法:
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;}
}发布于 2014-04-14 14:52:50
你试过..。
private MvcHtmlString GetCellHtml(HtmlHelper<TData> helper, TableColumn column, TData dataRow){
TagBuilder cellTagBuilder = new TagBuilder("td");
cellTagBuilder.InnerHtml = helper.Display(column.PropertyInfo.Name, dataRow);
...
}...?
如果只需要DisplayFor用于方法GetCellHtml,那么就不需要真正从PropertyInfo构建表达式。
发布于 2014-04-14 17:03:13
你可以这样做:
var properties = typeof (TModel).GetProperties();
foreach (PropertyInfo info in properties)
{
ParameterExpression p1 = Expression.Parameter(typeof(TModel), "m");
ParameterExpression p2 = Expression.Parameter(info.PropertyType, "m."+info.Name);
Expression<Func<TModel, dynamic>> exResult = Expression.Lambda<Func<TModel, dynamic>>(p1, p2);
helper.DisplayFor(exResult);
}抱歉花了一段时间。还得做些别的工作。
https://stackoverflow.com/questions/23060850
复制相似问题