gpt4 book ai didi

c# - 有没有比在 mvc razor View 中使用 if 语句显示带有角色的 html 代码更好的方法

转载 作者:太空宇宙 更新时间:2023-11-03 15:18:00 26 4
gpt4 key购买 nike

目前我的 C# 成员角色代码结构如下

@if (WebApiApplication.CurrentUser.CurrentRole == Role.SysAdmin || WebApiApplication.CurrentUser.CurrentRole == Role.Coordinator)
{
<li id="HomeMenu" class="@Model.GetMenuClass("HomeMenu")"><a href="@Url.Action("Index", "Home")"><i class="fa fa-home"></i>&nbsp;&nbsp;Home</a></li>
<li id="InternsMenu" class="@Model.GetMenuClass("InternsMenu")"><a href="@Url.Action("Index", "Interns")"><i class="fa fa-user"></i>&nbsp;&nbsp;Interns</a></li>
<li id="ProjectsMenu" class="@Model.GetMenuClass("ProjectsMenu")"><a href="@Url.Action("Index", "Projects")"><i class="fa fa-list-alt"></i>&nbsp;&nbsp;Projects</a></li>
<li id="AssignmentsMenu" class="@Model.GetMenuClass("AssignmentsMenu")"><a href="@Url.Action("Index", "Assignments")"><i class="fa fa-paperclip"></i>&nbsp;&nbsp;Assignments</a></li>
}
@if (WebApiApplication.CurrentUser.CurrentRole == Role.User )
{
<li id="HomeMenu" class="@Model.GetMenuClass("HomeMenu")"><a href="@Url.Action("Index", "Home")"><i class="fa fa-home"></i>&nbsp;&nbsp;Home</a></li>
<li id="InternsMenu" class="@Model.GetMenuClass("InternsMenu")"><a href="@Url.Action("Index", "Interns")"><i class="fa fa-user"></i>&nbsp;&nbsp;Interns</a></li>
<li id="ProjectsMenu" class="@Model.GetMenuClass("ProjectsMenu")"><a href="@Url.Action("Index", "Projects")"><i class="fa fa-list-alt"></i>&nbsp;&nbsp;Projects</a></li>
<li id="AssignmentsMenu" class="@Model.GetMenuClass("AssignmentsMenu")"><a href="@Url.Action("Index", "Assignments")"><i class="fa fa-paperclip"></i>&nbsp;&nbsp;Assignments</a></li>
}

@if (WebApiApplication.CurrentUser.CurrentRole == Role.Intern)
{
<li id="HomeMenu" class="@Model.GetMenuClass("HomeMenu")"><a href="@Url.Action("Index", "Home")"><i class="fa fa-home"></i>&nbsp;&nbsp;Home</a></li>
<li id="TimecardMenu" class="@Model.GetMenuClass("TimecardMenu")"><a href="@Url.Action("Timecard", "Assigments")"><i class="fa fa-user"></i>&nbsp;&nbsp;Interns</a></li>
<li id="FeedbackMenu" class="@Model.GetMenuClass("ProjectsMenu")"><a href="@Url.Action("Feedback", "Assigments")"><i class="fa fa-list-alt"></i>&nbsp;&nbsp;Projects</a></li>
<li id="SupportMenu" class="@Model.GetMenuClass("SupportMenu")"><a href="@Url.Action("Index", "Support")"><i class="fa fa-question-circle"></i>&nbsp;&nbsp;Support</a></li>
}

什么是处理 Roles 逻辑而不是 Razor View 的更好方法?在 C# 模型中执行?

最佳答案

我认为在 View 中处理这样的事情是可以的,因为它处理页面中菜单项的显示。

正如上面评论中提到的其他人一样,最好在 View 模型中定义映射到用户角色的属性并使用它。

您可以改用 switch 语句并将适用于不同角色的选项分组。例如,我确实在您的示例中注意到角色 SysAdmin、Coordinator 和 User 都具有相同的菜单项,所有角色都可以访问主菜单,只有角色 Intern 应该看到 TimeCard、反馈和支持菜单。

请看下面的例子:

<li id="HomeMenu" class="@Model.GetMenuClass("HomeMenu")"><a href="@Url.Action("Index", "Home")"><i class="fa fa-home"></i>&nbsp;&nbsp;Home</a></li>

@switch (WebApiApplication.CurrentUser.CurrentRole)
{
case Role.SysAdmin:
case Role.Coordinator:
case Role.User:
<li id="InternsMenu" class="@Model.GetMenuClass("InternsMenu")"><a href="@Url.Action("Index", "Interns")"><i class="fa fa-user"></i>&nbsp;&nbsp;Interns</a></li>
<li id="ProjectsMenu" class="@Model.GetMenuClass("ProjectsMenu")"><a href="@Url.Action("Index", "Projects")"><i class="fa fa-list-alt"></i>&nbsp;&nbsp;Projects</a></li>
<li id="AssignmentsMenu" class="@Model.GetMenuClass("AssignmentsMenu")"><a href="@Url.Action("Index", "Assignments")"><i class="fa fa-paperclip"></i>&nbsp;&nbsp;Assignments</a></li>
break;
case Role.Intern:
<li id="TimecardMenu" class="@Model.GetMenuClass("TimecardMenu")"><a href="@Url.Action("Timecard", "Assigments")"><i class="fa fa-user"></i>&nbsp;&nbsp;Interns</a></li>
<li id="FeedbackMenu" class="@Model.GetMenuClass("ProjectsMenu")"><a href="@Url.Action("Feedback", "Assigments")"><i class="fa fa-list-alt"></i>&nbsp;&nbsp;Projects</a></li>
<li id="SupportMenu" class="@Model.GetMenuClass("SupportMenu")"><a href="@Url.Action("Index", "Support")"><i class="fa fa-question-circle"></i>&nbsp;&nbsp;Support</a></li>
break;
}

关于c# - 有没有比在 mvc razor View 中使用 if 语句显示带有角色的 html 代码更好的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38257561/

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