我试图渲染的最终结果:
<input type="radio" name="options" id="options_1" />$1 - A Not Very Expensive Chocolate
<input type="radio" name="options" id="options_2" />$10 - A Kinda Expensive Chocolate
<input type="radio" name="options" id="options_3" />$100 - A Really Expensive Chocolate
从这段代码:
@foreach (var o in Model.Options){
<input type="radio" name="options" id=@("options_" + @o.ID) />$@o.PriceToAdd - @o.Label
}
如果我放弃'$'和' - '应该是简单的旧文本 - 东西是有效的。添加任何一个都会导致编译器警告和运行时错误。我已经尝试了explicit syntax as described here,但还没找到合适的组合。
答案 0 :(得分:2)
试试这样:
<击> 撞击>
<击>@foreach (var o in Model.Options) {
<input type="radio" name="options" id=@("options_" + @o.ID) />
<text>$</text>@o.PriceToAdd - @o.Label
}
击> <击>或: 击>
@foreach (var o in Model.Options) {
<input type="radio" name="options" id=@("options_" + @o.ID) />
@:$@o.PriceToAdd - @o.Label
}
或:
@foreach (var o in Model.Options) {
<input type="radio" name="options" id=@("options_" + @o.ID) />
@MvcHtmlString.Create("$")@o.PriceToAdd - @o.Label
}
或PriceToAdd
是否为数字:
@foreach (var o in Model.Options) {
<input type="radio" name="options" id=@("options_" + @o.ID) />
@o.PriceToAdd.ToString("c") - @o.Label
}