【发布时间】:2015-05-20 22:10:23
【问题描述】:
我正在使用一个新的 MVC 4 应用程序。基本上,顶部有几个菜单项,当前活动页面在我的_Layout.cshtml 中使用以下 JavaScript 突出显示:
<script type="text/javascript">
$(document).ready(function () {
$('#top-navbar .nav a[href="' + this.location.pathname + '"]').parent().addClass('active');
});
</script>
通过顶部的菜单导航时效果很好。但是,我添加了“登录”功能,如果用户在登录后尝试返回登录页面,它只会向他们发送Index 视图而不是Login 视图。问题是,CSS 仍然突出显示菜单中的 Login 按钮,而不是 Home 按钮。
我通过Session 状态保存用户的登录信息。这里是Login()ActionResult:
[HttpGet]
public ActionResult Login()
{
if (Session["ContactName"] != null)
{
return View("Index");
}
else
{
return View();
}
}
如您所见,如果ContactName 不是null,那么他们已经登录,所以它会向他们发送Home 视图。我应该改为Redirect() 到主页吗?
另外,如果需要,这里是菜单navbar:
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About Us", "About", "Home")</li>
<li>@Html.ActionLink("Customer Login", "Login", "Home")</li>
</ul>
【问题讨论】:
-
您应该返回
RedirectToAction("Index", "Home")。您所拥有的只是使用索引视图,但 url 仍将反映登录状态。
标签: javascript c# asp.net-mvc-4 razor