Inline If in Razor View

有些话、适合烂在心里 提交于 2020-01-02 01:29:10

问题


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.


回答1:


Try

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



回答2:


You could use something like this:

   @(true? "yes": "no") 



回答3:


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

@(If(testExpression, TruePart, FalsePart))



回答4:


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

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


来源:https://stackoverflow.com/questions/12391853/inline-if-in-razor-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!