我实现了一个通用的WebGrid类,它根据指定的(行)模型呈现其html标记。ASP.NET:通用列表中的剃刀
public class WebGrid<TRow> where TRow : WebGridRow{
public WebGrid(string tableId, IList<TRow> rows){
// Generate columsn from Model (TRow) by reflection
...
}
public MvcHtmlString GetHtml(HtmlHelper helper) {
return new MvcHtmlString(...);
}
}
public abstract class WebGridRow {
public virtual string GetRowId() {
return "row_" + Guid.NewGuid();
}
}
可以在模型类中定义布局,...以及属性。 例如:
public class MyRowModel : WebGridRow {
[CanFilter(false)]
[CssClass("foo")]
public string Name{get;set;}
[CanFilter(true)]
[CssClass("bar")]
public int SomeOtherProperty{get;set;}
}
现在我想创建一个通用视图,显示WebGridRow的亚类中的WebGrid的任何名单。问题是Razor不支持通用视图模型。
有没有人有一个想法我怎么能解决这个问题?
你所说的“剃刀不支持泛型观点的意思是楷模” ?您始终可以将视图的模型定义为WebGrid。 –
Whoami
2014-10-06 12:45:08
这是正确的。但是我无法将模型定义为WebGrid! –
Tobias
2014-10-06 12:48:59
什么是错误?因为我在视图中使用了很多泛型。您没有收到ICollection型号的视图吗?你确定你不错过你的观点(包含WebGrid 类)吗? –
Whoami
2014-10-06 12:51:57