我正在研究ASP.Net MVC 4應用程序。 _Layout主視圖包含一個菜單,我想根據您是否以用戶身份登錄而隱藏菜單中的某些項目,並在您以管理員身份登錄時進行顯示。如何隱藏/顯示基於登錄用戶的菜單項Asp.net mvc
我試過的方法確實隱藏了客戶端菜單中的鏈接選項卡,但是當我以管理員身份登錄時,它也隱藏了相同的鏈接選項卡,當我希望管理員查看它時。
僅舉我沒有任何作用或管理控制器的登錄是基於用戶
將提前得到一些幫助,謝謝。
<nav>
<ul id="menu">
<li>@Html.ActionLink("Rep Home", "Index" , "Audit")</li>
<li>@Html.ActionLink("Log Out", "Login" , "Home")</li>
@if (ViewContext.HttpContext.User.IsInRole("Admin"))
{
<li><a href="http://example/reports/?report=auditDetails" target="_blank">View your report</a></li>
}
</ul>
public class AccountController : Controller
{
//
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
FormsAuthentication.SetAuthCookie(model.UserName, false);
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
<forms loginUrl="~/User/Login" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
刪除你的[上一個問題](http://stackoverflow.com/questions/40929331/struggling-to-hide-show-menu-item-based-on-logged-users-asp-net-mvc) –
@StephenMuecke我沒有得到答案,我想ereaon爲什麼我發佈了一個新問題 – cazlouise
不要濫用本網站 - 要麼編輯其他問題,並刪除t他的一個,或者刪除另一個。 –