- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想为 ASP.NET 和 Razor 创建一种 WebGrid 2.0。
定义一个 ModelClass (TData),WebGrid 应该自己为表格创建 HTML。
TData 的属性应该通过反射读取 (typeof(TData).GetProperties)。属性的属性应该定义一些 css 和 html 样式甚至一些数据 (DisplayNameAttribute => ColumnHeader)。
现在我到了要点,当我想调用 htmlHelper.DisplayFor(...propertyInfoToExpression...) 来呈现数据内容时。
当我只获得数据(行)/模型和 propertyInfo 时,如何调用 DisplayFor?
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;}
}
最佳答案
你可以这样试试:
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);
}
抱歉,花了一些时间。不得不做一些其他的工作。
关于c# - PropertyInfo 到 Expression<Func<TModel, TProperty>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23060850/
我正在寻找一种方法来存储 Expression> 的集合用于对元素进行排序,然后针对 IQueryable 执行存储的列表对象(底层提供者是 Entity Framework )。 例如,我想做这样的
是否可以创建Expression>()它可以在不同的 htmlHelpers 中使用(例如在 CheckBoxFor() 中),如果我有一个模型对象 this HtmlHelper htmlHelpe
我的表达不是很好,我想改进它们,所以我想知道是否有人可以为我解释是否可以在类中创建一个属性,该属性可以在实例化过程中被赋予一个值,如下所示: new Column{ ColumnProperty =
我想为 ASP.NET 和 Razor 创建一种 WebGrid 2.0。 定义一个 ModelClass (TData),WebGrid 应该自己为表格创建 HTML。 TData 的属性应该通过反
我在 Entity Framework Generic Repository 包装器中有一个通用的 Include 方法。 public IQueryable Include(IQueryable q
我使用一些被序列化的强类型表达式,以允许我的 UI 代码具有强类型排序和搜索表达式。这些是类型 Expression>并按原样使用:SortOption.Field = (p => p.FirstNa
一个相对的 Java 新手问题。 我有一个带有提取器的 TableView 和一个 ListChangeListener添加到底层 ObservableList。 如果我有一个 StringPrope
以下代码产生错误: public static MvcHtmlString EditControlFor(this HtmlHelper htmlHelper, Expression> exp
以下是有效的,但我的 Body.NodeType 更改为 Convert,而不是 MemberAccess,这在获取 ModelMetadata.FromLambdaExpression 时出现问题:
首先,介绍一下背景。我正在使用 ASP.NET Web API 和 Entity Framework 5 开发 REST API,但是系统的要求是在我的 ApiController 和我的 DbCon
有没有办法转换属性选择器Expression>至 Expression>反之亦然?我已经知道如何转换为 Expression>使用... Expression> oldExp; Expression.
我是一名优秀的程序员,十分优秀!