我一直试图找到一种方法来访问母版页控件以显示/隐藏菜单选项。
在我的议员中,我有:
<div id="menucontainer">
<ul id="menu">
<li id="menuHome"><%= Html.ActionLink("Home", "Index", "Home")%></li>
<li id="menuNewHire"><%= Html.ActionLink("New Hire", "Index", "newHire")%></li>
<li><%= Html.ActionLink("Software", "Index", "Software")%></li>
<li><%= Html.ActionLink("Hardware", "Index", "Hardware")%></li>
<li><%= Html.ActionLink("Telecom", "Index", "Telecom")%></li>
<li><%= Html.ActionLink("About", "About", "Home")%> </li>
然后是一个ContentPlaceHolder ..我想要做的是在控制器中,查看用户是否在某个AD组中,如果是,则显示或隐藏其中一个菜单选项。在这种情况下,我只希望某些用户可以看到mnuNewHire。
关于如何做到这一点的任何想法?在此先感谢您的帮助。
...地理位置
答案 0 :(得分:3)
我会将此菜单外部化为一个单独的控制器并使用Html.Action辅助方法。例如,您可以拥有一个模型:
public class UserModel
{
public bool IsNoob { get; set; }
}
然后是控制器:
public class MenuController: Controller
{
private readonly IUsersRepository _repository;
public MenuController(IUsersRepository repository)
{
_repository = repository;
}
public ActionResult Index()
{
var username = User.Identity.Name;
var userModel = new UserModel
{
// Maybe you could use the membership provider here
// just don't know how you are handling authorization
IsNoob = _repository.GetUserGroup(username) == "Noobs"
}
return View(userModel);
}
}
和相应的局部视图:
<ul id="menu">
<li id="menuHome"><%= Html.ActionLink("Home", "Index", "Home")%></li>
<% if (Model.IsNoob) { %>
<li id="menuNewHire"><%= Html.ActionLink("New Hire", "Index", "newHire")%></li>
<% } %>
<li><%= Html.ActionLink("Software", "Index", "Software")%></li>
<li><%= Html.ActionLink("Hardware", "Index", "Hardware")%></li>
<li><%= Html.ActionLink("Telecom", "Index", "Telecom")%></li>
<li><%= Html.ActionLink("About", "About", "Home")%> </li>
</ul>
最后在您的母版页中:
<div id="menucontainer">
<%= Html.Action("index", "menu") %>
</div>