MVC动态导航栏

时间:2016-12-23 22:44:42

标签: c# asp.net .net asp.net-mvc model-view-controller

我正在创建一个MVC应用程序,但我希望导航栏对于不同类型的用户是不同的(对于某些用户,应该隐藏一些属性)。

@using Microsoft.AspNet.Identity
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Declaration System", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("Rules", "Rules", "Home")</li>
                    <li>@Html.ActionLink("Declare", "Declare", "Account")
                    </li>
                    <li>@Html.ActionLink("Set homework", "SetHomework", "Account")</li>
                    <li>@Html.ActionLink("My marks", "MyMarks", "Account")</li>
                    <li>@Html.ActionLink("Classes", "Classes", "Account")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                </ul>
                @Html.Partial("_LoginPartial")
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>© 2016 - Project by Yulia Buyanova and Maciej Miśkiewicz</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>
<style>
    footer {

     position:fixed;
        bottom:15px;
  }


</style>

我将我的用户类型存储在数据库中,因此我想检查当前记录的人是否为X类型,然后显示正确的导航栏。还有一个未登录的分离导航栏。我该怎么做? 编辑:
在正常的控制器中,我以这种方式传递实体:

public ActionResult MyMarks()
        {
            ClassDeclarationsDBEntities1 entities=new ClassDeclarationsDBEntities1();
            return View(entities.Users.ToList());
        }

但是它如何用于导航栏?

2 个答案:

答案 0 :(得分:2)

我会创建一个部分页面并将其命名为_NavigationMenuPartial并将此代码放在其中:

@model NavigationMenuModel

<ul class="nav navbar-nav">
    @if (this.Model.UserType == UserType.Admin) {
        <li>@Html.ActionLink( "Home", "Index", "Home" )</li>
    }
    <li>@Html.ActionLink( "Rules", "Rules", "Home" )</li>
    <li>@Html.ActionLink( "Declare", "Declare", "Account" )</li>
    <li>@Html.ActionLink( "Set homework", "SetHomework", "Account" )</li>
    <li>@Html.ActionLink( "My marks", "MyMarks", "Account" )</li>
    <li>@Html.ActionLink( "Classes", "Classes", "Account" )</li>
    <li>@Html.ActionLink( "Contact", "Contact", "Home" )</li>
</ul>

从控制器中将模型中的所需信息传递给您问题中的视图。然后从该模型中,您只能传递您需要的部分:

@Html.Partial( "_NavigationPartial", this.Model.NavigationMenuModel );

这是模型,但您可以根据需要以不同方式创建它:

public class NavigationMenuModel {
   public UserType UserType { get; set; }
}

public enum UserType {
   Admin = 1,
   Enquiry = 2
}

public class MarksModel {
   public List<User> Users { get; set; }
   public NavigationMenuModel NavigationMenuModel { get; set; }
}

控制器可以将模型传递给您问题中的视图,如下所示:

public ActionResult MyMarks() {
   ClassDeclarationsDBEntities1 entities = new ClassDeclarationsDBEntities1();
   var model = new MarksModel();
   model.Users = entities.Users.ToList();
   model.NavigationMenuModel = new NavigationMenuModel { UserType = UserType.Admin };
   return View( model );
}

我刚刚传递了UserType.Admin作为示例,但您可以传递您需要的任何内容,并从数据库中获取信息。

这也是一种干净的方法,因为与您的导航相关的所有内容都在局部视图中。

答案 1 :(得分:0)

您可以使用@ Html.RenderAction然后获取您想要的用户(缓存它),然后使用db中的模型填充部分视图。