Why does Razor not like this?

后端 未结 5 1451
闹比i
闹比i 2020-12-18 20:32

Ive got a mega annoying problem I have a view with:

@{

        if(ViewBag.Section == \"Home\")
        {
           
5条回答
  •  抹茶落季
    2020-12-18 20:55

    The simplest way to write this would be:

    Or, if you prefer, you can use a local variable:

    @{ var headerID = ViewBag.Section == "Home" ? "headerFrontPage" : "header"; }
    
    

    Regarding the more general case of unclosed tags in Razor code blocks, you can explicitly mark the opening tag as content:

    @if (ViewBag.Section == "Home")
    {
        @:
    } else { @:

提交回复
热议问题