asp.net-mvcasp.net-mvc-3razornerddinner

Parser error in mvc3 razor code


I'm getting this error in my cshtml file near this code

@{      
    var options = new OpenIdSelector();
    options.TextBox.LogOnText = "Log On";
}

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Unexpected "{" after "@" character. Once inside the body of a code block (@if {}, @{}, etc.) you do not need to use "@{" to switch to code.


@using DotNetOpenAuth.Mvc
@using DotNetOpenAuth.OpenId.RelyingParty
@{ 
 ViewBag.Title = "Log On"; 
}
<div id="login-oauth">
        <h3>via 3rd Party (recommended)</h3>
        
        @{ Html.RenderPartial("LogOnContent");}


    </div>
    <div id="or">OR</div>
    <div id="login-account">
        <h3>using a account</h3>
         </div>
    @using (Html.BeginForm())
    {
        <div>
            <p>
                <label for="username">Username:</label>
                @(Html.TextBox("username"))
                @(Html.ValidationMessage("username", "*"))
            </p>
            <p>
                <label for="password">Password:</label>
                @(Html.Password("password"))
                @(Html.ValidationMessage("password", "*"))
            </p>
            <p>
                @(Html.CheckBox("rememberMe")) <label class="inline" for="rememberMe">Remember me?</label>
            </p>
            <p>
                <input class="classiclogon" type="submit" value="Log On" />
            </p>
            <p>
                Please enter your username and password. @(Html.ActionLink("Register", "Register")) if you don't have an account.
            </p>
        </div>
   
    
        
 @{     
     {
            var options = new OpenIdSelector();
            options.TextBox.LogOnText = "Log On";
}
        }
       @MvcHtmlString.Create(Html.OpenIdSelectorScripts(options, null))
   @* @(Html.OpenIdSelectorScripts(this.Page, options, null))*@
    
    }

Solution

  • You're already in the

    @using (Html.BeginForm())
    {
    

    block, so your

    var options = new OpenIdSelector();
    options.TextBox.LogOnText = "Log On";
    

    block does not have to begin with @{. Pretty much what the error says.

    You can also just move those two lines to the code block at the top, where you set ViewBag.Title, to keep your HTML free from code as much as possible.