Jimmyt1988
Jimmyt1988

Reputation: 21126

If statement on one line including html inside it

I have the following code:

        <th class="
            @if( @Request.QueryString["desc"].AsBool() ){ @:table-desc }
            @if( @Request.QueryString["desc"].AsBool() ){ @:table-desc }
        " width="100%" @Html.SortTableClickEvent(@Request.Path, "Name", Convert.ToBoolean(@Request.QueryString["desc"]))>
            Name
        </th>

It doesn't like the @if statement all being on one line, the "@:table-desc" thinks that the closing bracket "}" is part of the html.

Any ideas how to get around this?

Parser Error Message: The if block is missing a closing "}" character.  Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.

Upvotes: 2

Views: 293

Answers (2)

Nitin Varpe
Nitin Varpe

Reputation: 10694

Try using

<th class="@Request.QueryString["desc"].AsBool()?table-desc:table-desc".....

Or use something like below which i have implemented in my application

<th class="@if (Request.QueryString["desc"].AsBool())
             {<text>trueclass</text>}
            else       
            {text>falseclass</text>}" >

Upvotes: 0

CodeCaster
CodeCaster

Reputation: 151594

You want to print a string, so:

@if( @Request.QueryString["desc"].AsBool() ){ @":table-desc" }

You can also stick it in a variable:

@{
    var sortClass = Request.QueryString["desc"].AsBool() 
        ? "table-desc" 
        : "table-asc";
}

<th class="@sortClass">

Upvotes: 2

Related Questions