How can I hide a WebGrid column based on the current user role?

I need to make a grid using webgrid, and I would like to hide the column (title and elements) of the editing actions based on the user role.

How can I do this using webgrid?

+3
source share
1 answer

You can write a helper method that will generate 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(); } } 

and then, in your opinion:

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

Source: https://habr.com/ru/post/1397815/


All Articles