arame3333
arame3333

Reputation: 10193

HTML5, Razor. How do I get different styling for the header element?

I am new to HTML5. This code returns a parser error on the if statement. I do not know if it will work anyway, so how do I fix the if statement and how do I get this to work?

<div class="page">
    @if (Constants.EnvironmentSetting.ToUpper() == "LIVE"){
        <header>
    } else {
        <header class="headerTest">
    }

        <div id="title">
            @Content.Image("MulalleyLogoSmall.jpg", "float:left;padding:10px 10px 0 10px", Url)
            <div class="head" style="float:left;padding-top:4px;">Instruction To Open Contract (ITOC)</div>
        </div>

        <div id="logindisplay">
            @Html.Partial("_LogOnUserControl")
        </div> 

        <nav>
            @Html.Partial("_MenuItems")
        </nav>
    </header>

    <section>
        @RenderBody()
    </section>
</div>

Upvotes: 1

Views: 442

Answers (2)

Tom Chantler
Tom Chantler

Reputation: 14941

You don't need to put the curly brace on its own line.

What happens if you change it to this?

@if (Constants.EnvironmentSetting.ToUpper() == "LIVE"){
    @:<header>
} else {
    @:<header class="headerTest">
}

The @: tells the Razor parser that the rest of the line should be taken as markup and not C# code.

Upvotes: 2

Eric J.
Eric J.

Reputation: 150118

You have to place the { on its own line in Razor

@if (Constants.EnvironmentSetting.ToUpper() == "LIVE"){

should be

@if (Constants.EnvironmentSetting.ToUpper() == "LIVE")
{

Otherwise, that portion of the code should work.

On a side note, it's better to use

Constants.EnvironmentSettings.Equals("LIVE", 
             StringComparison.CurrentCultureIgnoreCase);

The ToUpper method is often used to convert a string to uppercase so that it can be used in a case-insensitive comparison. A better method to perform case-insensitive comparison is to call a string comparison method that has a StringComparison parameter whose value you set to StringComparison.CurrentCultureIgnoreCase for a culture-sensitive, case-insensitive comparison.

http://msdn.microsoft.com/en-us/library/ewdd6aed.aspx

Upvotes: 1

Related Questions