开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜