Inline If in Razor View

2019-04-22 20:11发布

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.

4条回答
趁早两清
2楼-- · 2019-04-22 20:43

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

@IIf(String.IsNullOrEmpty(sortOrder), "Name desc", "")
查看更多
来,给爷笑一个
3楼-- · 2019-04-22 20:57

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

@(If(testExpression, TruePart, FalsePart))
查看更多
虎瘦雄心在
4楼-- · 2019-04-22 21:07

Try

@Code
    @(If(True, true, true))
End Code
查看更多
可以哭但决不认输i
5楼-- · 2019-04-22 21:07

You could use something like this:

   @(true? "yes": "no") 
查看更多
登录 后发表回答