9

In my controller, I have and inline If statement:

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

In my view, I can't seem to use inline if:

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

It says, "If must end with matching End If." Why can't I use an inline if here? Thanks.

user1477388
  • 20,790
  • 32
  • 144
  • 264

4 Answers4

12

Try

@Code
    @(If(True, true, true))
End Code
Tim B James
  • 20,084
  • 4
  • 73
  • 103
  • 2
    This looks like the answer. I wonder why it needs the parenthesis? Thanks for this. – user1477388 Sep 12 '12 at 15:48
  • 1
    Probably just a work around to get it working. Alternatively use `IIf(true, "", "")` – Tim B James Sep 12 '12 at 15:49
  • 1
    IIF is obsolete, to my knowledge http://stackoverflow.com/questions/28377/iif-vs-if – user1477388 Sep 12 '12 at 15:49
  • 2
    Also, I believe IIF works differently from IF in that it will execute both true and false statements and will not short circuit like normal if will. – user1477388 Sep 12 '12 at 15:56
  • 1
    The main difference for me between IF and IIF is that IF needs a nullable reference type, and IIF just needs an expression which evaluates to a Boolean. Useful when working with Boolean model properties (which arent nullable) – Steven Ryssaert Sep 17 '15 at 12:26
12

You could use something like this:

   @(true? "yes": "no") 
Big Daddy
  • 5,160
  • 5
  • 46
  • 76
4

You can do an inline if in vb.net like this:

@(If(testExpression, TruePart, FalsePart))
shekky
  • 139
  • 10
0

You could use IIf, you don't need to clutter your code with @Code sections:

@IIf(String.IsNullOrEmpty(sortOrder), "Name desc", "")
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928