我有一个webgrid,有一个列我想要只有某些用户可见.
目前我已经编码了网格如下
目前我已经编码了网格如下
if (Context.User.IsInRole(Role.Inputter) || Context.User.IsInRole(Role.Administrator)) { @grid.GetHtml(columns: grid.Columns( grid.Column(format: (item) => Html.ActionLink("Select","Details",new { contractId = item.ContractId })),grid.Column(format: (item) => Html.ActionLink("Edit","Edit",grid.Column("SignOffDate","Sign Off Date",format: @<text> <span>@item.SignOffDate.ToString("d/M/yyyy")</span></text>),grid.Column("FullContractNumber","Contract Number"),grid.Column("ContractTitle","Title") )); } else { @grid.GetHtml(columns: grid.Columns( grid.Column(format: (item) => Html.ActionLink("Select","Title") )); }
但是肯定有一个更好的方法,不重复所有的代码?
2列输入之间的唯一区别是我想显示特定用户的编辑链接.那么最好的替代方法是什么呢?
解决方法
尝试这样(未经测试,目前无法访问VS):
@{ var gridColumns = new List<WebGridColumn>(); gridColumns.Add(grid.Column(format: (item) => Html.ActionLink("Select",new { contractId = item.ContractId }))); if (Context.User.IsInRole(Role.Inputter) || Context.User.IsInRole(Role.Administrator)) { gridColumns.Add(grid.Column(format: (item) => Html.ActionLink("Edit",new { contractId = item.ContractId }))); } gridColumns.Add(grid.Column("SignOffDate",format: @<text> <span>@item.SignOffDate.ToString("d/M/yyyy")</span></text>)); gridColumns.Add(grid.Column("FullContractNumber","Contract Number")); gridColumns.Add(grid.Column("ContractTitle","Title")); } @grid.GetHtml(columns: grid.Columns(gridColumns.ToArray()));