有没有一种方法来终止MVC剃刀@:行? [英] Is there a way to terminate MVC Razor @: line?
问题描述
考虑这个例子:
<input type="checkbox" @if (flag) { @: checked } />
问题是,} /方式&gt;
被剃刀引擎视为输出字符串的一部分
The problem is that the } />
is conceived by the Razor engine as part of the output string.
我试图用括号但没有运气。
I've tried to use parenthesis but no luck.
有没有办法来终止 @:在同一行
运营商,不然我就必须把它拆分到其它线路/使用三元运算符
Is there a way to terminate the @:
operator in same line, or I'll have to split it to other line / use ternary operator?
我的要求的一个非常普遍的用例可以添加类,如果值是true:
A very common use-case of my request can be adding a class if a value is true:
<div class='container @(active ? "active")'/>
因此,如果:
三元运营商是不是present,它把它把它当作一元如果
运营商,但是这只是一个愿望...
So if the :
of the ternary operator isn't present, it treats it treats it as a unary if
operator, but that's just a wish...
<一个href=\"http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/7288182-add-unary-ternary-operator\"相对=nofollow>在这里是我对C#UserVoice的建议。
Here's my suggestion on C# UserVoice.
推荐答案
有关,您可以使用 @value
语法,也有复选框特殊情况(见单个属性< A HREF =http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/相对=nofollow>剃刀快速参考):
For single attribute you can use @value
syntax, that also have special case for checkbox (see Razor quick reference ):
<input type="checkbox" checked="@flag" />
您也可以使用&LT;文本&GT;
语法,提供了明确的(而不是将结束行)结束标记:
You also can use <text>
syntax that provides explicit (instead of "up to end of the line") closing tag:
<input type="checkbox" @if (flag) { <text>checked</text> } />
这篇关于有没有一种方法来终止MVC剃刀@:行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!