开发者

How to deal with apparently mismatched HTML tags in Razor Engine?

开发者 https://www.devze.com 2023-02-21 00:41 出处:网络
Here is the case: @{ if (A) { <div> <span>bla</span> <!-- This is where it breaks! -->

Here is the case:

@{
    if (A)
    {
        <div>
            <span>bla</span>
      <!-- This is where it breaks! -->
    }
    else
    {
            <span>bla</span>
    }
    if(B)
    {
        </div>
    }
}

Since the <div> isn't closed inside the same condition where it's opened, Razor Engine thinks that the else statement is HTML mark-up. What could I possibly do in situations like this to m开发者_如何学JAVAake it work properly?


The problem is mismatched html tags. The easy way around this is to use the @: parser directive

@{
    bool A = true;
    bool B = true;

    if (A)
    {
        @:<div>
            <span>bla</span>
      <!-- This is where it breaks! -->
    }
    else
    {
            <span>bla</span>
    }
    if(B) {
        @:</div>
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消