【问题标题】:How can I hide a WebGrid column based on the current user's role?如何根据当前用户的角色隐藏 WebGrid 列?
【发布时间】:2011-07-06 20:13:38
【问题描述】:

我需要使用 webgrid 做一个网格,我想根据用户角色隐藏编辑操作的列(标题和项目)。

如何使用 webgrid 做到这一点?

【问题讨论】:

    标签: asp.net-mvc-3 webgrid


    【解决方案1】:

    您可以编写一个辅助方法,该方法将根据用户角色动态生成列:

    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)))
    

    【讨论】:

    • 谢谢!我会使用这个想法
    猜你喜欢
    • 2019-05-17
    • 2018-07-19
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 2018-08-09
    相关资源
    最近更新 更多