javascript ASP.NET MVC 4 默认 _LoginPartial 模板注销不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20348810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC 4 Default _LoginPartial template Logout not working
提问by Gman16
I have been fiddling around with the _Layout and _PartialLayouts of the default MVC 4 templates and suddenly the 'Logout' feature in the '_PartialLogin' doc has stopeed working. To give you more info, the _LoginPartial.cshtml is called from the _NavBar.cshtml which in turn is called from the _Layout.cshtml
我一直在摆弄默认 MVC 4 模板的 _Layout 和 _PartialLayouts,突然“_PartialLogin”文档中的“注销”功能停止工作。为了给您更多信息,_LoginPartial.cshtml 是从 _NavBar.cshtml 调用的,而 _NavBar.cshtml 又是从 _Layout.cshtml 调用的
The code of the _LoginPartial.cshtml is:
_LoginPartial.cshtml 的代码是:
@if (Request.IsAuthenticated) {
<text>
<p>Hello, @Html.ActionLink(User.Identity.Name, "Manage", "Account", routeValues: null, htmlAttributes: new { @class = "username", title = "Manage" })!
@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {
@Html.AntiForgeryToken()
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
}</p>
</text>
}
The code from the _NavBar.cshtml is:
_NavBar.cshtml 中的代码是:
<form class ="navbar-form navbar-right" method="post" action="/account/login">
<div class ="form-group">
@Html.Partial("_LoginPartial")
</div>
</form>
The code from the _Layout.cshtml is:
_Layout.cshtml 中的代码是:
<body>
@Html.Partial("_Navbar")
@RenderSection("featured", required: false)
@RenderBody()
<div class="container">
I get that maybe the problem is the javascript has been disabled unintentionally somehow. Am I missing some script tags maybe? Or is it something in the previous class where I called the _PartialLogin from??
我知道问题可能是 javascript 以某种方式无意中被禁用了。我是否缺少一些脚本标签?或者是我在上一堂课中调用 _PartialLogin 的地方??
回答by Bart Calixto
I'm not answering your question ( you already solved the problem) but I think this greatly improves the code:
我不是在回答你的问题(你已经解决了问题)但我认为这大大改进了代码:
change this line :
改变这一行:
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
with this one:
有了这个:
<button class="btn btn-link">Log off</button>
also the id on the form is not needed with this change.
此更改也不需要表单上的 id。
回答by red8884
I had this problem too - because we are also using bootstrap.
我也有这个问题 - 因为我们也在使用引导程序。
This worked fine in Chrome:
这在 Chrome 中运行良好:
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
But to get it working on Explorer, the solution was to move the javascript to the onclick attribute:
但是为了让它在资源管理器上工作,解决方案是将 javascript 移动到 onclick 属性:
<a href="#" onclick="javascript:document.getElementById('logoutForm').submit()">Log off</a>
<a href="#" onclick="javascript:document.getElementById('logoutForm').submit()">Log off</a>
回答by Gman16
Solved the problem...
解决了问题...
You cannot put the javascript text in a <form>
tag. It will not work as the bootstrap messes with it then.
您不能将 javascript 文本放在<form>
标签中。它不会工作,因为引导程序会弄乱它。
It doesnt matter how many times you call the partialLayouts within eachother - just be careful which tags you embed the javascript in
您在彼此中调用 partialLayouts 多少次并不重要 - 请注意您将 javascript 嵌入到哪些标签中