Change how [DataType.Currency] renders html

谁说我不能喝 提交于 2019-12-23 18:29:57

问题


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>

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&nbsp;4.139,99</div>

回答1:


You can use a custom display format (instead of the "Currency" attribute):

[DisplayFormat(DataFormatString = "$U&nbsp;{0:#,###0.00}")]

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.




回答2:


Use this annotation: [DisplayFormat(DataFormatString = "{0:0}")]



来源:https://stackoverflow.com/questions/18523145/change-how-datatype-currency-renders-html

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