[英]ASP.NET MVC 4 Default _LoginPartial template Logout not working
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. 我一直在摆弄默认MVC 4模板的_Layout和_PartialLayouts,突然之间'_PartialLogin'文档中的'Logout'功能已经停止工作。 To give you more info, the _LoginPartial.cshtml is called from the _NavBar.cshtml which in turn is called from the _Layout.cshtml
为了给你提供更多信息,从_NavBar.cshtml调用_LoginPartial.cshtml,然后从_Layout.cshtml调用_NavBar.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. 我明白了问题可能是javascript已经无意中被无意识地禁用了。 Am I missing some script tags maybe?
我可能错过了一些脚本标签吗? Or is it something in the previous class where I called the _PartialLogin from??
或者它是上一课中我称之为_PartialLogin的东西?
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。
I had this problem too - because we are also using bootstrap. 我也有这个问题 - 因为我们也在使用bootstrap。
This worked fine in Chrome: 这在Chrome中运行良好:
<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: 但是为了让它在Explorer上运行,解决方案是将javascript移动到onclick属性:
<a href="#" onclick="javascript:document.getElementById('logoutForm').submit()">Log off</a>
https://stackoverflow.com/a/17078223 https://stackoverflow.com/a/17078223
Solved the problem... 解决了这个问题......
You cannot put the javascript text in a <form>
tag. 您不能将javascript文本放在
<form>
标记中。 It will not work as the bootstrap messes with it then. 然后,它将无法作为引导程序混乱。
It doesnt matter how many times you call the partialLayouts within eachother - just be careful which tags you embed the javascript in 无论你多少次在同一个地方调用partialLayouts - 只要注意你嵌入了javascript的标签
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.