gpt4 book ai didi

asp.net-mvc - 根据 MVC 中的角色隐藏表中的列

转载 作者:行者123 更新时间:2023-12-02 18:13:01 24 4
gpt4 key购买 nike

在索引 View 中的一些表中,某些角色可以比其他角色看到更多的表内容。

所以这意味着我应该隐藏一些列

我这样做又快又脏:

<% bool codesZichtbaar = Roles.IsUserInRole("Admin") || Roles.IsUserInRole("Algemeen Beheer") || Roles.IsUserInRole("Secretariaat");  %>


<table>
<tr>
<th>
</th>
<th>
Adres
</th>
<th>
Instellingsnr.
</th>
<% if (codesZichtbaar) { %>
<th>
Codes
</th>
<%} %>
<th>
IKON
</th>
<th>
Datum Van-Tot
</th>
<th>
</th>
</tr>
<% foreach (var item in Model) { %>
<tr class="inst<%: item.actieveSchool?"active":"inactive" %>">
<td>
<% Html.RenderPartial("buttons", new MVC2_NASTEST.Models.ButtonDetail() { RouteValues = new { id = item.Inst_ID }, edit = false, details = true, delete = false, takeOptionalsFromUrl = true }); %>
</td>
<td>
<%: item.INST_NAAM%><br />
<%: item.STRAAT%><br />
<%: item.POSTCODE%>
<%: item.GEMEENTE%>
</td>
<td>
<%: item.DOSSNR%>
</td>
<% if (codesZichtbaar) { %>
<td>
Gebruiker:
<%: item.Inst_Gebruikersnaam%><br />
C:
<%: item.Inst_Concode%><br />
D:
<%: item.Inst_DirCode%>
</td>
<%} %>
<td>
T:
<%: item.INST_TYPE%><br />
Inst_REF:
<%: item.INST_REF%><br />
Loc_REF:
<%: item.INST_LOC_REF%><br />
Loc_Nr:
<%: item.INST_LOCNR%>
</td>
<td>
<%: String.Format("{0:d}", item.DATUM_VAN)%>
-
<%: String.Format("{0:d}", item.DATUM_TOT)%>
</td>
<td>
<a href='<%= Url.Action("Links", "Instelling", MVC2_NASTEST.RouteValues.MergeRouteValues(MVC2_NASTEST.RouteValues.optionalParamters(Request.QueryString), new {id=item.Inst_ID})) %>'
title="Linken">
<img src="<%= Url.Content("~/img/link.png") %>" alt="Link" width="16" /></a>
</td>
</tr>
<% } %>
</table>

但是这是MVC,我的代码很枯燥,正确的方法是什么?或者,什么是更干净的方法来做到这一点。

最佳答案

我会为此编写一个 Html 助手。它可能看起来像这样:

public bool IsInRole(this HtmlHelper instance, params string[] roles)
{
var user = html.ViewContext.HttpContext.User;
foreach(var role in roles)
{
if(user.IsInRole(role))
return true;
}

return false;
}

如果用户处于任何提供的角色中,此函数将返回 true。你可以像这样使用它:

<tr>
<td>First - Visible to all</td>
<% if(Html.IsInRole("Admin", "Algemeen Beheer", "Secretariaat")) { %>
<td>Only for privileged users</td>
<% } %>
<td>Last - Visible to all
</tr>

这是一种轻量、干净且可重复使用的方法。

关于asp.net-mvc - 根据 MVC 中的角色隐藏表中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4649795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com