简体   繁体   中英

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. To give you more info, the _LoginPartial.cshtml is called from the _NavBar.cshtml which in turn is called from the _Layout.cshtml

The code of the _LoginPartial.cshtml is:

@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:

<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:

<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??

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.

I had this problem too - because we are also using bootstrap.

This worked fine in 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:

<a href="#" onclick="javascript:document.getElementById('logoutForm').submit()">Log off</a>

https://stackoverflow.com/a/17078223

Solved the problem...

You cannot put the javascript text in a <form> tag. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM