Leons
Leons

Reputation: 2674

ASP.NET MVC compilation error with Razor syntax

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

Upvotes: 1

Views: 1320

Answers (2)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93494

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?

Upvotes: 1

Chris Fulstow
Chris Fulstow

Reputation: 41902

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

Upvotes: 1

Related Questions