0

I'm having an interesting problem. That there seems to be no answer to after googling.

Using a slightly customized version of the _LoginPartial.cshtml I seem to be getting the attached error when clicking logout. Haven't seen this behavior and would love some help if anybody can figure it out.

_LoginPartial.cshtml

@if (Request.IsAuthenticated)
{

    <ul class="nav navbar-nav navbar-right" style="margin-right: 15px;">
        <li>
            @Html.ActionLink("Hello " + ViewData["FullName"] + "!", "Index",    "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
    </ul>

}
else
{
    <ul class="nav navbar-nav navbar-right" style="margin-right: 15px;">
        <li>@Html.ActionLink("Log In", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
    </ul>
}

Error Message LogoutError

3
  • It's quite obvious that there is no element in the document with the id it is looking for. Why that is missing cannot be deduced from the files you have shown here. Commented Feb 24, 2016 at 19:51
  • We will need to see some more of your code in the _LoginPartial.cshtml file. Your example is not showing a <form> or @Html.BeginForm() element with the ID logoutForm. Commented Feb 24, 2016 at 19:55
  • Your customization removed some critical code. Both of the submitted answers give good solutions. Commented Jan 20, 2017 at 17:11

2 Answers 2

1

All you need is just a POST to the Logout action.

You can add back the blank form in pure html:

<form action="Account/Logout" id="logoutForm"><form>

The Log off link doesn't have to be inside the form. In case you have some css rules to mess up your hyperlink inside the <form> tag.

Translating to Razor. You can do

    using (Html.BeginForm("LogOff", "Account", new { area = "" }, FormMethod.Post, new { id = "logoutForm" })) ...

like the other answer suggested.

Or just keep it simple in Html, or just

<form action='@Url.Action("Logout","Account")' id="logoutForm"><form>
Sign up to request clarification or add additional context in comments.

Comments

1

To make this work you should update your form and add BeginForm on it. It should look something like this:

@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Account", new { area = "" }, FormMethod.Post, new { id = "logoutForm" }))
    {

    <ul class="nav navbar-nav navbar-right" style="margin-right: 15px;">
        <li>
            @Html.ActionLink("Hello " + ViewData["FullName"] + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
    </ul>
    }
}
else
{
    <ul class="nav navbar-nav navbar-right" style="margin-right: 15px;">
        <li>@Html.ActionLink("Log In", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
    </ul>
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.