1

I want to ask about dynamic menu based on role in master page asp.net mvc 4. before i ask about it. I have database menu in sql server express 2008 like this:

Id_Menu        int          NotNull
Displaymenu    varchar(50)  Null
Url            varchar(50)  Null
ParentID       int          Not Null

and i have created master page in mvc 4 like this :

<body>
    <header>
       <div class="float-left">
               <img src="../../Images/logo.png" width="350" height="95" />
        </div>
        <div class="content-wrapper">
            <div class="float-right">
                <section id="login">
                    <%: Html.Partial("_LoginPartial") %>
                </section>
                <nav>
                    <ul id="menu">
                        <li><%: Html.ActionLink("Home", "Index", "Home") %></li>
                        <li><%: Html.ActionLink("About", "About", "Home") %></li>
                        <li><%: Html.ActionLink("Contact", "Contact", "Home") %></li>
                    </ul>
                </nav>
            </div>
        </div>
    </header>
    <div id="body">
        <asp:ContentPlaceHolder ID="FeaturedContent" runat="server" />
        <section class="content-wrapper main-content clear-fix">
            <asp:ContentPlaceHolder ID="MainContent" runat="server" />
        </section>
    </div>
    <footer>
        <div class="content-wrapper">
            <div class="float-left">
                <p>&copy; <%: DateTime.Now.Year %> - Cenie.Sistem Informasi.IT 
      Telkom</p>
            </div>
        </div>
    </footer>

and the i have created nested master page like this :

<%@ Master Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
 AutoEventWireup="true" CodeBehind="Dashboard.master.cs" 
 Inherits="TA_SID.Views.Shared.Dashboard<TA_SID.Models>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
 </asp:Content>
 <asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
  </asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<div class="span3 main-menu-span">
            <div class="well nav-collapse sidebar-nav">
                <ul class="nav nav-tabs nav-stacked main-menu">
                    <li class="nav-header hidden-tablet">MAIN    
   MENU</li>
                    if()
                    <li><a class="ajax-link"   
href="/Admin/Index"><i class="icon-home"></i><span class="hidden-tablet"> 
Dashboard</span>   
</a></li>
                    <li><a class="ajax-link"   
 href="/Admin/PertaminaIndex"><i class="icon-eye-open"></i><span class="hidden-tablet"> 
Pertamina</span></a></li>
                    <li><a class="ajax-link"     
href="/Admin/DistributorIndex"><i class="icon-edit"></i><span class="hidden-tablet"> 
Distributor</span></a></li>
                    <li><a class="ajax-link"   
href="/Admin/CustomerIndex"><i class="icon-list-alt"></i><span class="hidden-tablet">    
 Customer</span></a></li>
                    <li><a class="ajax-link"    
 href="/Admin/ProdukIndex"><i class="icon-font"></i><span class="hidden-tablet">  
 Product</span></a></li>
                    <li><a class="ajax-link" href="table.html">   
 <i class="icon-align-justify"></i><span class="hidden-tablet"> Chart</span></a></li>
                    <li><a class="ajax-link"   
 href="calendar.html"><i class="icon-calendar"></i><span class="hidden-tablet">   
 Calendar</span></a></li>
                    <li><a class="ajax-link" href="grid.html"><i  
 class="icon-th"></i><span class="hidden-tablet"> User</span></a></li>

                </ul>
            </div><!--/.well -->
        </div>
 </asp:Content>
 <asp:Content ID="Content4" ContentPlaceHolderID="ScriptsSection" runat="server">
 </asp:Content>

can you help me.. How to create dynamic menu load in nested master page if i log in as admin or as user after i success log in as role?

thank you for your help..

4

2 回答 2

4

首先,您不想在母版页中执行此逻辑。您想要创建一个局部视图,该视图将根据某个控制器中的子操作返回的模型信息构建菜单。例如:

您应该将此逻辑移动到一些负责构建导航的控制器中。在此控制器内部,您应该声明一个返回所需信息的子操作。

如果您计划将 URL 和描述存储在数据库中,那么您也可以将访问权限存储在表中。这样,您可以传递用户的组或用户名并检索该用户有权访问的 URL 集合。之后,您可以使用类似于下面的代码。

例如:

public class NavigationController : Controller
{
    [ChildActionOnly]
    public PartialViewResult GetMenuForUser()
    {
        var model = _securityLayer.GetUrlForUser(HttpContext.User.Identity.Name);

        return PartialView("_UserMenu", model);
    }
}

您的 _UserMenu.cshtml 可能类似于:

@model IEnumerable<SomeCollection>

@foreach(var url in Model) {
    <li><a class="ajax-link" href="@url.Url">@url.Description</a></li>
}

然后在您的母版页中,您想要使用您将调用的菜单:

<div class="well nav-collapse sidebar-nav">
    <ul class="nav nav-tabs nav-stacked main-menu">
        <li class="nav-header hidden-tablet">MAIN MENU</li>
        @{ Html.RenderAction("GetMenuForUser", "Navigation"); }
    </ul>
</div>

无论您是否将用户访问权限存储在数据库中,这都应该让您了解如何进行操作。

更新:对不起,我后来意识到您使用的是 ASPX View Engine 而不是 Razor。您需要将我的标记转换为 ASPX View Engine。

于 2013-06-29T21:39:07.773 回答
0

尝试创建一个控制器,在其中使用身份获取当前用户角色的 IList,然后将该列表转换为用逗号分隔的字符串,然后根据您的偏好将其存储在 viewbag 或 tempdata 中,然后在布局中使用 Viewbag 数据显示项目基于在那里找到的角色。这是控制器

        [Authorize]
    public ActionResult Index()
    {
        if (User.Identity.IsAuthenticated)
        {
            var user = User.Identity;
            ViewBag.Name = user.Name;
            ViewBag.DisplayMenu = UserRole();
            return View();
        }
        else
        {
            ViewBag.Name = "Not Logged in";
        }
        return View();
    }
    public string UserRole()
    {
        string urole = "non";
        if (User.Identity.IsAuthenticated)
        {
            var user = User.Identity;
            ApplicationDbContext context = new ApplicationDbContext();
            var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            var s = UserManager.GetRoles(user.GetUserId());
            urole = String.Join(",", s.ToArray());
        }
        return urole;
    }
于 2016-08-04T07:06:18.307 回答