2014-10-06 2 views
0

지정된 (행) 모델에 따라 html 태그를 렌더링하는 일반 WebGrid 클래스를 구현했습니다.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;} 

} 

이 지금은 일반 뷰를 만들려면, 즉 WebGrid로 WebGridRow의 서브 클래스의 목록을 보여줍니다. 문제는 Razor가 일반 뷰 모델을 지원하지 않는다는 것입니다.

누구든지 어떻게 해결할 수 있습니까?

+0

을 도움이 될 것입니다 모델 "? 뷰의 모델을 항상 WebGrid 으로 정의 할 수 있습니다. – Whoami

+0

맞습니다. 하지만 모델을 WebGrid로 정의 할 수는 없습니다 ! – Tobias

+0

무엇이 오류입니까? 내가보기에서 많은 제네릭을 사용했기 때문에. 보기 ICollection 유형의 모델을 받고있어? 보기 (WebGrid 클래스를 포함하는보기)에서 사용하지 않는 것이 확실합니까? – Whoami

답변

1

이것은 당신이 면도기는 일반보기를 지원하지 않습니다 "무엇을 의미합니까 당신에게

모델

public interface IWebGrid 
{ 
    MvcHtmlString GetHtml(HtmlHelper helper); 
} 

public class WebGrid<TRow> : IWebGrid where TRow : WebGridRow 
{ 
    private ICollection<TRow> Rows {get;set;} 

    public WebGrid(string tableId, IList<TRow> rows) 
    { 
     // Generate columns from Model (TRow) by reflection and add them to the rows property 
    } 

    public MvcHtmlString GetHtml(HtmlHelper helper) 
    { 
     string returnString = "Generate opening tags for the table itself"; 
     foreach(TRow row in this.Rows) 
     { 
      // Generate html for every row 
      returnString += row.GetHtml(helper); 
     } 
     returnString += "Generate closing tags for the table itself"; 
     return MvcHtmlString.Create(returnString); 
    } 
} 

public abstract class WebGridRow 
{ 
    public virtual string GetRowId() 
    { 
     return "row_" + Guid.NewGuid(); 
    } 

    public abstract MvcHtmlString GetHtml(HtmlHelper helper); 
} 

public class MyRowModel : WebGridRow 
{ 
    [CanFilter(false)] 
    [CssClass("foo")] 
    public string Name{get;set;} 

    [CanFilter(true)] 
    [CssClass("bar")] 
    public int SomeOtherProperty{get;set;} 

    public override MvcHtmlString GetHtml(HtmlHelper helper) 
    { 
     // Generate string for the row itself 
    } 
} 

보기 (디스플레이 템플릿 여부)

@model IWebGrid 
@model.GetHtml(this.Html); 
+0

좋아 보인다. 그러나 그리드가 모델이기 때문에 그리드가 단지 컨트롤이기 때문에 틀린 것입니다. 또는 나는 틀린 무엇을 이해 했느냐? – Tobias

+0

DataGrid 컨트롤을 사용하고 싶지 않습니다. 왜 그리드가 그려지는지 GetHtml() 함수를 원하십니까? 자신의 태그를 사용하여 직접 그리는 객체로 사용하지 않습니까? – Whoami

+0

예 : 인터페이스는 이동 수단입니다! – Tobias