我正在创建一个MVC应用程序,但我希望导航栏针对不同类型的用户有所不同(对于某些用户,应该隐藏某些属性)。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型的,然后显示正确的导航栏。还有一个未登录的分隔栏导航栏,我该怎么做?
编辑:
在正常conrollers我通过实体是这样的:
public ActionResult MyMarks()
{
ClassDeclarationsDBEntities1 entities=new ClassDeclarationsDBEntities1();
return View(entities.Users.ToList());
}
但它是如何为导航栏工作?
你不能只使用一个简单的'if'语句?例如'@if(userType ==“Admin”){...}' – DavidG
@DavidG如何将我的数据库的所有实体都传递给此,以便我可以访问数据库并检查用户类型? –
为什么你需要通过所有的实体?您只需要传入当前用户类型。 – DavidG