询问可能很愚蠢,但我仍然无法正确使用Google。以下是我要打印"选择"的代码:
<option value="0" @if(ViewBag.country == 0) { @:selected } >NO</option>
它给我带来了错误,例如
Parser Error Message: The if block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.
推荐:https://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/
您可以使用?:inline代码符号
<option value="0" @(ViewBag.country == 0 ? "selected" : string.Empty)>NO</option>
可以像这样完成
创建像这样的值
@{ var selected = ViewBag.country == 0 ? "selected" : ""; }
,然后创建如下
的选项<option value="0" @selected>NO</option>
,如本文所述。
通过使用 @:代码块中的字符序列。
要使您的代码工作将卷曲括号}
带到下一行。
<option value="0" @if (ViewBag.country == 0) { @: selected
}>
NO
</option>
其他选项使用<text> element
:
<option value="0" @if (ViewBag.country == 0) { <text> selected</text> }>NO</option>
或
<option value="0" @{if (ViewBag.country == 0) { <text> selected</text> }}>NO</option>
参考:
1. shorten-这是伊斯兰教 -/razor-to-One-in-line
2.一个线路,如果是/net-razor-and-net