Dollar sign and/or Dash breaking Razor's parser

你离开我真会死。 提交于 2020-01-02 18:56:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!