2011-03-01 65 views

回答

2

你可以寫一個輔助方法,它會根據用戶角色動態生成列:

public static class GridExtensions 
{ 
    public static WebGridColumn[] RoleBasedColumns(
     this HtmlHelper htmlHelper, 
     WebGrid grid 
    ) 
    { 
     var user = htmlHelper.ViewContext.HttpContext.User; 
     var columns = new List<WebGridColumn>(); 

     // The Prop1 column would be visible to all users 
     columns.Add(grid.Column("Prop1")); 

     if (user.IsInRole("foo")) 
     { 
      // The Prop2 column would be visible only to users 
      // in the foo role 
      columns.Add(grid.Column("Prop2")); 
     } 
     return columns.ToArray(); 
    } 
} 

,然後在您的視圖:

@{ 
    var grid = new WebGrid(Model); 
} 
@grid.GetHtml(columns: grid.Columns(Html.RoleBasedColumns(grid))) 
+0

謝謝!我會用這個想法 – 2011-03-02 12:25:12

相關問題