问题
the end-result i'm trying to render:
<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
From this code:
@foreach (var o in Model.Options){
<input type="radio" name="options" id=@("options_" + @o.ID) />$@o.PriceToAdd - @o.Label
}
If i drop both the '$' and the '-' from what should be plain old text - stuff works. Adding either resulted in compiler warnings and runtime errors. I've tried the explicit syntax as described here but haven't found the right combination yet.
回答1:
Try like this:
@foreach (var o in Model.Options) {
<input type="radio" name="options" id=@("options_" + @o.ID) />
<text>$</text>@o.PriceToAdd - @o.Label
}
or:
@foreach (var o in Model.Options) {
<input type="radio" name="options" id=@("options_" + @o.ID) />
@:$@o.PriceToAdd - @o.Label
}
or:
@foreach (var o in Model.Options) {
<input type="radio" name="options" id=@("options_" + @o.ID) />
@MvcHtmlString.Create("$")@o.PriceToAdd - @o.Label
}
or if PriceToAdd
is numeric:
@foreach (var o in Model.Options) {
<input type="radio" name="options" id=@("options_" + @o.ID) />
@o.PriceToAdd.ToString("c") - @o.Label
}
来源:https://stackoverflow.com/questions/4562192/dollar-sign-and-or-dash-breaking-razors-parser