实现:左边为菜单导航,当一个菜单中包含多个Tabs,并且不同的Tab要根据权限的不同显示。

1.前台代码

//当点击左侧菜单时,将访问Controller中的Home方法,这样就会根据用户权限的不同,通过后台的判断来决定显示的页面
<li class="@(ViewBag.SelectedNavIndex == NavMenuCatalog.StudentContacts ? "active" : string.Empty)"> @(Html.NavListActionLink<ContactController>("Student Contacts", c => c.Home(), "icon-list", (bool)(ViewBag.SelectedNavIndex == NavMenuCatalog.StudentContacts))) </li>

2.后台代码

//在Controller中创建一个Home方法,根据权限的不同让其跳转到不同的Action,从而展现不同的页面
public
ActionResult Home() { if (User.PortalIdentity.AuthorizedActions.Any(x => x == ActionLevelSecurity.StudentAddress.GetStringValue())) { return this.RedirectToAction(c => c.StudentAddress()); } if (User.PortalIdentity.AuthorizedActions.Any(x => x == ActionLevelSecurity.ManageContact.GetStringValue())) { return this.RedirectToAction(c => c.Select()); } return new EmptyResult(); }

3.总结

  问题:

    点击该菜单时,默认会显示第一个Tab,如果此时没有Home这个Action,而是将前台代码中指定为一个具体的Action,例如c => c.StudentAddress(), 当有的用户没有StudentAddress的权限时,默认要显示第二个Tab,这个时候页面显示就不正确。

  技巧:

    在Controller中加入一个Home Action,让其根据权限判断要显示的页面很好地解决了这个问题。
  

原文地址:https://www.cnblogs.com/sunshineground/p/4331935.html