admin管理员组文章数量:1024664
For example, I display some text in the view:
@:<link href="/Content/stylesheet.css" rel="stylesheet" type="text/css" />
@:<script src="/bundles/scripts" type="text/javascript"></script>
What is @:
and when and how should I use it?
For example, I display some text in the view:
@:<link href="/Content/stylesheet.css" rel="stylesheet" type="text/css" />
@:<script src="/bundles/scripts" type="text/javascript"></script>
What is @:
and when and how should I use it?
1 Answer
Reset to default 5This operator is useful in conjunction with other Razor server side operators when you want to output something as a literal text.
For example:
@if (model.Foo) {
@:Some text to be written directly.
}
You can't just write this because it would produce an error:
@if (model.Foo) {
Some text to be written directly.
}
So basically for your example where the output is already HTML tags you don't need this operator. But when you want to mix some server side Razor markup with an explicit output then you could use it. You could also use the special <text>
tag to achieve the same effect:
@if (model.Foo) {
<text>
Some text to be written directly.
</text>
}
This will trick the Razor parser to treat the text that you want to output to the HTML as literal and not consider it as server side code.
Another example. Let's suppose that you want to output some HTML tag that doesn't have a corresponding opening tag:
@if (model.Foo) {
@:</div>
}
If you just wrote this:
@if (model.Foo) {
</div>
}
then the Razor parser would have plained as it wouldn't know about an opening div tag.
For example, I display some text in the view:
@:<link href="/Content/stylesheet.css" rel="stylesheet" type="text/css" />
@:<script src="/bundles/scripts" type="text/javascript"></script>
What is @:
and when and how should I use it?
For example, I display some text in the view:
@:<link href="/Content/stylesheet.css" rel="stylesheet" type="text/css" />
@:<script src="/bundles/scripts" type="text/javascript"></script>
What is @:
and when and how should I use it?
1 Answer
Reset to default 5This operator is useful in conjunction with other Razor server side operators when you want to output something as a literal text.
For example:
@if (model.Foo) {
@:Some text to be written directly.
}
You can't just write this because it would produce an error:
@if (model.Foo) {
Some text to be written directly.
}
So basically for your example where the output is already HTML tags you don't need this operator. But when you want to mix some server side Razor markup with an explicit output then you could use it. You could also use the special <text>
tag to achieve the same effect:
@if (model.Foo) {
<text>
Some text to be written directly.
</text>
}
This will trick the Razor parser to treat the text that you want to output to the HTML as literal and not consider it as server side code.
Another example. Let's suppose that you want to output some HTML tag that doesn't have a corresponding opening tag:
@if (model.Foo) {
@:</div>
}
If you just wrote this:
@if (model.Foo) {
</div>
}
then the Razor parser would have plained as it wouldn't know about an opening div tag.
本文标签: javascriptHow to use ofin cshtmlStack Overflow
版权声明:本文标题:javascript - How to use of @: in .cshtml - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745620206a2159527.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论