在我们的ASP.NET Core 1.1
项目之一中,在呈现View
后,我收到以下错误。我想我正确地关注SO
用户的建议,例如@DarinDimitrov
here
错误:Input string was not in correct format
视图模型:
Public Class OrdersViewModel
{
....
[DisplayFormat(DataFormatString = "{(0:C)}")]
public float? price { get; set; }
}
查看:
@model MyProj.Models.OrdersViewModel
@{
Layout = "";
}
....
<tr>
<td>Price:</td>
<td>@Html.DisplayFor(t => t.price)</td>
</tr>
更新:
此@String.Format("{0:c}", Model.price)
按@nemesv
的建议here工作。但我不想在视图中使用它,因为ModelView在各种视图的多个位置使用。所以我想继续在这些地方使用DisplayFor(...)
- 因为它有点简单。
答案 0 :(得分:0)
System.FormatException
您拥有意味着您的DataFormatString
错误。在您的情况下,您需要从格式字符串中删除()
(显式调用String.Format
可以使用不同的模板):
[DisplayFormat(DataFormatString = "{0:C}")]
public float? price { get; set; }
如果您需要将结果字符串设为(123.45)
,而不是123.45
,那么
[DisplayFormat(DataFormatString = "({0:C})")]