4

有什麼辦法使用MVC3 WebGrid擴展的DataAnnotations屬性?WebGrid和EF4屬性

我使用一些EF4 DataAnnotations屬性,如[ScaffoldColumn(true)]和[Display(Description =「Name」,Prompt =「Enter name」,ShortName =「Name」)]]。但它對WebGrid沒有任何影響。

那麼如果可以使用WebGrid的DataAnnotations屬性?


更新行,達林季米特洛夫compitly回答我的問題。而且它是+1。 但是對於工作而言,MVCContrib比WebGrid好得多。這是我的選擇。

回答

6

WebGrid幫助程序不使用DataAnnotations。如果你想讓這些屬性有一些效果,你可以自定義這些列並在其中使用編輯器/顯示模板。你也可以有一些線:

@{ 
    var grid = new WebGrid(Model); 
} 

@grid.GetHtml(
    columns: ViewData 
       .ModelMetadata 
       .Properties 
       .Single() 
       .Properties 
       .Select(
        p => grid.Column(
         columnName: p.PropertyName, 
         header: p.ShortDisplayName 
        ) 
       ) 
) 
+0

謝謝。任何例子? – FFire 2011-02-12 13:14:15