gpt4 book ai didi

c# - MVC 动态导航栏

转载 作者:行者123 更新时间:2023-11-30 23:17:40 25 4
gpt4 key购买 nike

我正在创建一个 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 类型,然后显示正确的导航栏。还有一个用于未登录的分隔导航栏。我该怎么做?
编辑:
在普通 Controller 中,我以这种方式传递实体:

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

但是它如何用于导航栏?

最佳答案

我会创建一个部分页面并将其命名为 _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>

从 Controller 将模型中的所需信息传递到问题中的 View 。然后从那个模型中你可以只传递你的部分需要的部分,如下所示:

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

Controller 可以将模型传递给您问题中的 View ,如下所示:

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 作为示例,但您可以传递所需的任何内容,并从数据库中获取信息。

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

关于c# - MVC 动态导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41308609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com