请参阅以下创建菜单项的代码。现在,我如何显示/隐藏/访问如下创建的菜单项。另外我想确保用户不直接访问页面。我想根据我的表中的值(没有默认表)来执行此操作。我知道我们可以根据角色使用[Authorize(Roles = "Admin")]
,但就像我提到的,我想手动在自定义表中查找值,然后显示/隐藏/允许访问页面。
感谢。
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Roles", "Index", "AspNetRoles", new { }, new { @style = "color:#21ce99;" }) </li>
<li>@Html.ActionLink("Users", "Index", "AspNetUsers", new { }, new { @style = "color:#21ce99;" })</li>
<li>@Html.ActionLink("User Groups", "Index", "UserGroupRoles", new { }, new { @style = "color:#21ce99;" })</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
注意:在得到Stephen的帮助后,我设法编写了以下代码。但现在我收到了错误
“路径'/'的控制器未找到或未实现IController。”
控制器代码
using System.Web.Mvc;
using .Models;
namespace TestProj.Controllers
{
public class NavigationVMController : Controller
{
[ChildActionOnly]
public ActionResult Navigation()
{
NavigationVM NavItems = new NavigationVM();
NavItems.CanViewRoles = true; //setting all true temporarily for testing
NavItems.CanViewUsers = true;
NavItems.CanViewUserGroups = true;
//return View(Alex);
return View("_Navigation", NavItems);
}
// GET: NavigationVM
public ActionResult Index()
{
return View();
}
}
}
型号代码
namespace TestProj.Models
{
public class NavigationVM
{
public bool CanViewRoles { get; set; }
public bool CanViewUsers { get; set; }
public bool CanViewUserGroups { get; set; }
}
}
部分查看代码
@model TestProj.Models.NavigationVM
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
@if (Model.CanViewRoles)
{
<li>@Html.ActionLink("Roles", "Index", "AspNetRoles", new { }, new { @style = "color:#21ce99;" }) </li>
}
<li>@Html.ActionLink("Users", "Index", "AspNetUsers", new { }, new { @style = "color:#21ce99;" })</li>
<li>@Html.ActionLink("User Groups", "Index", "UserGroupRoles", new { }, new { @style = "color:#21ce99;" })</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
从_Layout.cshtml调用代码
@Html.Action("Navigation", "NavigationVMController")
答案 0 :(得分:2)
为导航链接创建局部视图,并调用ChildActionOnly
方法,该方法根据可用于有条件地检查应显示哪些链接的某些参数初始化视图模型
查看模型
public class NavigationVM
{
public bool CanViewRoles { get; set; }
public bool CanViewUsers { get; set; }
public bool CanViewUserGroups { get; set; }
}
控制器(可能在BaseController中,因此它随处可用
[ChildActionOnly]
public ActionResult Navigation
{
NavigationVM model = new NavigationVM();
model.canViewRoles = ?? // call a service which returns the value based on the current user
....
return PartialView("_Navigation", model)
}
部分视图(_Navigation.cshtml)
@model yourAssembly.NavigationVM
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
@if (Model.CanViewRoles)
{
<li>@Html.ActionLink("Roles", "Index", "AspNetRoles", new { }, new { @style = "color:#333;" })</li>
}
.... // ditto for CanViewUsers and CanViewUserGroups
</ul>
</div>
并在主视图或布局中
@Html.Action("Navigation") // renders the navigation links
@Html.Partial("_LoginPartial")
这只会显示相应的链接,但当然用户仍然可以输入地址来尝试访问页面,这样您的控制器方法仍然需要执行验证
AspNetRolesController
public ActionResult Index()
{
// Check permissions based on user
bool canView = ?? // call a service which returns the value based on the current user
if (!canView)
{
return new HttpUnauthorizedResult();
}
....
}