-1

I'm quite confused in Razor syntax ))) For example I have an element:

<div class="category-block">

</div>

I have a parameter

string viewMode = Html.ViewContext.HttpContext.Request.QueryString["view"];

and I need to add list-block class to my div element with class category-block and to get

<div class="category-block list-block">

</div>

if

viewMode != null && viewMode.Equals("list", StringComparison.OrdinalIgnoreCase).

I trie to do something like:

<div class="category-block @(viewMode != null && viewMode.Equals("list", StringComparison.OrdinalIgnoreCase)) ? list-block : string.Empty ">

but every time I get a lot of syntax errors... I don't know is it possible to write something inside opened attribure brackets;

Dmytro
  • 16,668
  • 27
  • 80
  • 130
  • possible duplicate of [How to use ? : if statements with Razor and inline code blocks](http://stackoverflow.com/questions/4770605/how-to-use-if-statements-with-razor-and-inline-code-blocks) – CD.. Oct 21 '12 at 12:31

1 Answers1

5

You've got your final ) in the wrong place. Also, you need to add quotes around list-block.

<div class="category-block @(viewMode != null && viewMode.Equals("list", StringComparison.OrdinalIgnoreCase) ? "list-block" : string.Empty)">

Also, if you're going to be doing a lot of string comparison in the view like that, I'd recommend creating a helper method so that you don't have to use the verbose string.Equals(str, StringComparison.OrdinalIgnoreCase) every time.

Chris
  • 27,596
  • 25
  • 124
  • 225