开发者

Turn menu options on/off from Masterpage depending on user privs

开发者 https://www.devze.com 2023-02-06 13:18 出处:网络
I have been trying to find a way to access a master page control in order to show/hide a menu option.

I have been trying to find a way to access a master page control in order to show/hide a menu option.

In my MP, I have:

   <div id="menucontainer">
        &nbsp;
            <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>

Then a ContentPlaceHolder after that..What I want to do is in the controller, see if a user is in a certain AD group, if so, show or hide one of the menu options. In this case, mnuNewHire I only want to be visible to certain users.

Any ideas on how to do this?开发者_运维百科 Thanks in advance for any and all help.

Geo...


I would externalize this menu into a separate controller and use Html.Action helper method. So for example you could have a model:

public class UserModel
{
    public bool IsNoob { get; set; }
}

then a controller:

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);
    }
}

and a corresponding partial view:

<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>

and finally in your master page:

<div id="menucontainer">
    <%= Html.Action("index", "menu") %>
</div>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号