asp.net mvc 3 - Hiding columns values for few records in the grid depending on their role type: MVC3 -
**i trying create view has grid
view layout, using is:**
@model ienumerable<vc.mc.reportalweb.ui.users> @using myspace @{ viewbag.title = "users"; var grid = new webgrid(source: model, cansort: true); } <h2>users</h2> <p> @html.actionlink("create new", "create") </p> @grid.gethtml(columns: grid.columns( grid.column("username"), grid.column("email"), grid.column( header: "", style: "text-align-center", format: (item) => new htmlstring(html.actionlink("edit", "edit", new { id = item.id ).tostring() + " | " + html.actionlink("details", "details", new { id = item.id }).tostring() + " | " + html.actionlink("delete", "delete", new { id = item.id }).tostring())) ) ) @grid.gethtml(columns: grid.columns(html.rolebasedcolumns(grid))) @{ if (!string.isnullorwhitespace(grid.sortcolumn)) { <script type="text/javascript"> $('thead > tr > th > a[href*="sort=@grid.sortcolumn"]').parent().append('@(grid.sortdirection == sortdirection.ascending ? "^" : "v")'); </script> } }
the rolebasedcolumns(grid) helper method in razor is
public static webgridcolumn[] rolebasedcolumns( htmlhelper htmlhelper, webgrid grid ) { var user = htmlhelper.viewcontext.httpcontext.user; var columns = new list<webgridcolumn>(); var query = p in _adminmodelcontainer.users select p; ilist<users> userlist = query.tolist(); (int = 0; < userlist.count; i++) { // prop1 column visible users columns.add(grid.column("username")); if (userlist[i].rolesid == 1) { // prop2 column visible users // in foo role columns.add(grid.column("email")); } } return columns.toarray(); }
i want show edit , delete link buttons users rolesid 1.
using above functionality grid replicating .columns headers shown rolesid 1.
i in fix.
any of great use.
thanks
do easy way , use telerik's free open source mvc controls (the grid) , when define grid.. use dynamic column along lines of:
@(html.telerik().grid(model) .name("grid").tablehtmlattributes(new { width="800"}) .columns(columns => { if (userisinwhateverrole){ columns.template(o => html.action(generateyourlinkstuffhere)); } columns.bound(o => o.address).width(150); columns.bound(o => o.city).width(120); columns.bound(o => o.state).width(100); }) .sortable() .scrollable() .groupable() .filterable() .pageable(paging => paging.pagesize(5) .style(gridpagerstyles.nextpreviousandnumeric) .position(gridpagerposition.bottom)))
no other grid nice in opinion - , free. why give headache : )
Comments
Post a Comment