我實現了一個通用的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