ASP.NET MVC 4 Default _LoginPartial template Logout not working

Gman16 picture Gman16 · Dec 3, 2013 · Viewed 11.9k times · Source

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

Answer

Bart Calixto picture Bart Calixto · Jul 15, 2014

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.