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;
You’ve got your final ) in the wrong place. Also, you need to add quotes around
list-block.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.