Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 271
Gman16 Avatar asked Dec 03 '13 10:12

Gman16


2 Answers

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.

like image 167
Bart Calixto Avatar answered Sep 28 '22 05:09

Bart Calixto


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

like image 31
red8884 Avatar answered Sep 28 '22 05:09

red8884