开发者

Outputting literal HTML between razor statements results in compilation error

I have a foo object, and I want to output:

Title, Location

So I try...:

@if (sometruestuff){
    @foo.Title, @f开发者_运维知识库oo.Location
}

@if (sometruestuff){
    @foo.Title , @foo.Location
}

Both fail to compile.

However...:

@if (sometruestuff){
    @foo.Title<span>,</span> @foo.Location
}

...works.

Is there some trick I am missing?

Edit: This happens inside a codeblock, updated to reflect this.


You could escape the , using the @: because the Razor parser considers it as part of the server side code and if you want to output the comma as is in the HTML it needs to be escaped:

@if (sometruestuff){
    @foo.Title@:, @foo.Location
}


It seems you are inside a code block, because otherwise it should normally work. You can try this:

<text>
@foo.Title, @foo.Location
</text>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜