asp.net-mvc 如果有 HTML 内容,我可以编写内联吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16443308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:49:07  来源:igfitidea点击:

Can I write an inline if with HTML content?

asp.net-mvcrazorinline-if

提问by Santiago

I want to write something like:

我想写一些类似的东西:

@( checkCondition ? "<span class='label'>Right!</span>" : "")

But it is showing the source code instead the HTML, there is a easy way to do this?

但它显示的是源代码而不是 HTML,有一种简单的方法可以做到这一点?

Thank you!

谢谢!

回答by Volodymyr Machula

You can use @Html.Raw(mystring)method like this:

您可以使用这样的@Html.Raw(mystring)方法:

@( checkCondition ? Html.Raw("<span class='label'>Right!</span>") : Html.Raw(""))

回答by Mish Ochu

You can be even more concise (granted harder to read) with this:

你可以更简洁(更难阅读):

@Html.Raw(checkCondition ? "<span class='label'>Right!</span>": string.Empty)

回答by A. Morel

We can also do like that:

我们也可以这样做:

@if (checkCondition ) { <text><span class='label'>Right!</span></text> }

The text tag allows you to write html with syntax highlighting!

text 标签允许您编写带有语法突出显示的 html!