Inline If in a Razor view

In my controller, I have a built-in if statement:

ViewBag.NameSortParam = If(String.IsNullOrEmpty(sortOrder), "Name desc", "") 

In my opinion, I can not use the built-in if:

 @Code If(True, true, true) End code 

It says, "If you need to finish using End End If." Why can't I use the inline if here? Thanks.

+7
source share
4 answers

Try

 @Code @(If(True, true, true)) End Code 
+10
source

You can use something like this:

  @(true? "yes": "no") 
+9
source

You can do inline if in vb.net:

 @(If(testExpression, TruePart, FalsePart)) 
+3
source

You can use IIf , you do not need to clutter up your code with @Code sections:

 @IIf(String.IsNullOrEmpty(sortOrder), "Name desc", "") 
0
source

All Articles