如何根据当前用户的角色隐藏WebGrid列? [英] How can I hide a WebGrid column based on the current user's role?
本文介绍了如何根据当前用户的角色隐藏WebGrid列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用webgrid进行网格处理,我想根据用户角色隐藏编辑操作的列(标题和项目).
I need to do a grid using webgrid and I would like to hide the column (header and items) of edit actions based on user role.
我该如何使用webgrid?
How can I do that with webgrid?
推荐答案
您可以编写一个帮助程序方法,该方法将根据用户角色动态生成列:
You could write a helper method which would generate the columns dynamically based on user roles:
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)))
这篇关于如何根据当前用户的角色隐藏WebGrid列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文