更改[DataType.Currency]呈现html的方式 [英] Change how [DataType.Currency] renders html
问题描述
当前,将DataAnnotation货币应用于属性时,然后使用DisplayFor将其呈现为html:
Currently, when applying the DataAnnotation Currency to a property, it then using DisplayFor renders this as the html :
cshtml:
<div>@Html.DisplayFor(m=>m.Price)</div>
html:
<div>$U 4.193,99</div>
如何更改它,所以货币符号不是空格
?
我知道我可以将CSS应用于div的空白:nowrap;但最好只更改货币字符串格式以获取:
How can I change it so the currency symbol is not white-spaced
?
I know I can apply a css to the div for white-space:nowrap; but it would be better if I can just change the currency string format to get :
<div>$U 4.139,99</div>
推荐答案
您可以使用自定义显示格式(而不是货币属性):
You can use a custom display format (instead of the "Currency" attribute):
[DisplayFormat(DataFormatString = "$U {0:#,###0.00}")]
示例: http://rextester.com/PQSV4120 (请注意,示例代码保存后, nbsp;文本将被删除。您可以手动将其放回,然后重新运行。)
Example: http://rextester.com/PQSV4120 (Note that the "nbsp;" text is removed when the example code saves. You can manually put it back in though, and re-run it.)
编辑:更改了格式字符串并添加了示例。
EDIT: Changed format string and added example.
这篇关于更改[DataType.Currency]呈现html的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!