我的页面将显示一些货币数据。我想用货币格式格式化数据,但只使用显示模板。
我有以下代码:
@foreach (var item in Model.Data)
{
<tr class="@(item.Group%2==0? "odd-colore": "even-colore")">
<td>@Html.DisplayFor(modelItem => item.Name)</td>
<td>@Html.DisplayFor(modelItem => item.Amount1)</td>
<td>@Html.DisplayFor(modelItem => item.LName)</td>
<td>@Html.DisplayFor(modelItem => item.Amount2)</td>
<td>@Html.DisplayFor(modelItem => item.Amount3)</td>
</tr>
}
我创建了一个DisplayTemplate
String.cshtml
, 因为我的数据类型是字符串:
@model string
@{
IFormatProvider formatProvider = new System.Globalization.CultureInfo("en-US");
<span class="currency">@Model.ToString("C",formatProvider)</span>
}
但是当我运行它时,我得到了错误:
方法“ToString”没有重载需要 2 个参数
如何使用DisplayTemplate
或string.Format("{0:C}")