1

The following code snippet is causing a compilation error that I am unable to pinpoint. I tried removing the 2 from the last brace. I tried the AntiForgery without braces - no luck.

The error is CS1501: No overload for method 'Write' takes 0 arguments

Any suggestions?

<div id="header-wrapper">
    <div id="header">
        <div id="menu-wrapper">
            @if (Page.User.Identity.IsAuthenticated){
                @Html.Telerik().Menu().Name("mainnav").BindTo("main").Effects(x => x.Toggle())
            }     
        </div>
        @if (Page.User.Identity.IsAuthenticated){
            <form action="@Url.Action("Logoff", "Account")" id="formLogout" method="POST">
                @{ Html.AntiForgeryToken(); }
                <a href="" id="logout" onclick="$('#formLogout').submit();return false;"></a>
            </form>
        @}
        <div id="subnav"></div>
    </div>
</div>

enter image description here

2 Answers 2

1

Try replacing:

@{ Html.AntiForgeryToken(); }

with:

@Html.AntiForgeryToken()

(and remove the @ from your if closing brace)

Code should be:

@if (Page.User.Identity.IsAuthenticated) {
  <form action="@Url.Action("Logoff", "Account")" id="formLogout" method="POST">
    @Html.AntiForgeryToken()
    <a href="" id="logout" onclick="$('#formLogout').submit();return false;"></a>
  </form>
}
Sign up to request clarification or add additional context in comments.

1 Comment

That worked! I was sure that I already tried it, but I obviously did not.
1

Since Chris answered your problem, I'd like to point out several improvements to your code. Your code could be written this way:

@if (Page.User.Identity.IsAuthenticated){
 using (Html.BeginForm("Logoff", "Account", FormMethod.Post, new { id="formLogout" }) {
     Html.AntiForgeryToken();
     <a href="" id="logout">Logout</a>
 }
}

<script type="text/javascript">
    $(function() {
        $('#formLogout').click(function() { 
            $('#formLogout').submit(); return false; 
        });
    }};
</script>

This does a few things. First, it simplifies your markup. This is known as "unobtrusive" javascript, because it removes all those ugly "onclick" and other kinds of javascript related events.

Secondly, it seperates your javascript logic from your markup, making it easier to maintain.

Notice also that i'm using the BeginForm html helper, which simplifies your markup as well. Notice how there's only a single @ sign in the entire block?

1 Comment

This is so much cleaner and definitely easier to maintain. +1 for making the improvement suggestions.

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.