I'm using MVC3 with a detailed view that needs to display a formatted dollar amounts like
I'm using MVC3 with a detailed view that needs to display a formatted dollar amounts like $1,200.00. The controller is passing in a double value in this case for MonthlyMortgage to the view. The line of code below however, does not display the correct string formatting. What is displayed is $1200.00, and what I need is $1,200.00.
我正在使用带有详细视图的MVC3,需要显示格式化的美元金额,如$ 1,200.00。在这种情况下,控制器将MonthlyMortgage传递给视图。但是,下面的代码行不会显示正确的字符串格式。显示的是$ 1200.00,我需要的是$ 1,200.00。
I have tried:
我努力了:
[email protected]("{0:c}", Html.DisplayFor(model => model.MonthlyMortgage))
and I have tried this:
我试过这个:
[email protected]("{0:#,###,###,##0.000}", Html.DisplayFor(model => model.MonthlyMortgage))
Can someone please enplane why this is not working?
有人可以告诉我为什么这不起作用?
2 个解决方案
#1
11
You don't need to use Html.DisplayFor because it will return MvcHtmlString so the string.Format doesn't apply.
您不需要使用Html.DisplayFor,因为它将返回MvcHtmlString,因此string.Format不适用。
Just use the string.Format on your model:
只需在模型上使用string.Format:
@String.Format("{0:c}", Model.MonthlyMortgage)
Note you don't need the '$' sign anymore because the {0:c} will take care of it.
请注意,您不再需要“$”符号,因为{0:c}会处理它。
#2
6
@nemesv has a direct answer. The other option would be to remove String.Format() from the View and use DataAnnotations to attach a formatting to MonthlyMortgage.
@nemesv有一个直接的答案。另一种选择是从View中删除String.Format()并使用DataAnnotations将格式附加到MonthlyMortgage。
An example from MSDN:
来自MSDN的一个例子:
[DisplayFormat(DataFormatString = "{0:C}")]
public Decimal ListPrice { get; set; }
And btw, #,###,###,##0.000 can be shortened to #,##0.00
btw,#,###,###,## 0.000可缩短为#,## 0.00
,200.00. The controller is passing in a double value in this case for MonthlyMortgage to the view. The line of code below however, does not display the correct string formatting. What is displayed is 00.00, and what I need is
I'm using MVC3 with a detailed view that needs to display a formatted dollar amounts like $1,200.00. The controller is passing in a double value in this case for MonthlyMortgage to the view. The line of code below however, does not display the correct string formatting. What is displayed is $1200.00, and what I need is $1,200.00.
我正在使用带有详细视图的MVC3,需要显示格式化的美元金额,如$ 1,200.00。在这种情况下,控制器将MonthlyMortgage传递给视图。但是,下面的代码行不会显示正确的字符串格式。显示的是$ 1200.00,我需要的是$ 1,200.00。
I have tried:
我努力了:
[email protected]("{0:c}", Html.DisplayFor(model => model.MonthlyMortgage))
and I have tried this:
我试过这个:
[email protected]("{0:#,###,###,##0.000}", Html.DisplayFor(model => model.MonthlyMortgage))
Can someone please enplane why this is not working?
有人可以告诉我为什么这不起作用?
2 个解决方案
#1
11
You don't need to use Html.DisplayFor because it will return MvcHtmlString so the string.Format doesn't apply.
您不需要使用Html.DisplayFor,因为它将返回MvcHtmlString,因此string.Format不适用。
Just use the string.Format on your model:
只需在模型上使用string.Format:
@String.Format("{0:c}", Model.MonthlyMortgage)
Note you don't need the '$' sign anymore because the {0:c} will take care of it.
请注意,您不再需要“$”符号,因为{0:c}会处理它。
#2
6
@nemesv has a direct answer. The other option would be to remove String.Format() from the View and use DataAnnotations to attach a formatting to MonthlyMortgage.
@nemesv有一个直接的答案。另一种选择是从View中删除String.Format()并使用DataAnnotations将格式附加到MonthlyMortgage。
An example from MSDN:
来自MSDN的一个例子:
[DisplayFormat(DataFormatString = "{0:C}")]
public Decimal ListPrice { get; set; }
And btw, #,###,###,##0.000 can be shortened to #,##0.00
btw,#,###,###,## 0.000可缩短为#,## 0.00
,200.00.I'm using MVC3 with a detailed view that needs