我的剃刀代码中有一个表,该表循环遍历模型中的所有项目,并为每个项目创建一个表行。我试图弄清楚如何将日期格式化为MM / dd / yyyy,而不是默认的“ MM / dd / yyyy HH:mm:ss”格式。
<table class="table-condensed">
<thead>
<tr>
<th>Company</th>
<th>Contract No</th>
<th>Description</th>
<th>Expires</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@Html.ActionLink(@item.CompanyName, "ViewContract", new { contractID = @item.ID }) </td>
<td width="200">@item.ContractNumber</td>
<td>@item.Description</td>
<td>@item.ExpireDate</td>
</tr>
}
</tbody>
</table>
我已经尝试过@ item.ExpireDate.ToString(“ MM / dd / yyyy”),但是它抛出一个错误,说.ToString没有参数。
我正在使用PagedList NuGet程序包,它使HTML帮助程序调用有所不同。通过在数据模型构造函数中指定日期格式,然后使用显示帮助器,我能够格式化日期。
在数据模型中:
[DisplayFormat(DataFormatString = "{0: MM/dd/yyyy}")]
public Nullable<System.DateTime> ExpireDate { get; set; }
在剃须刀中:
<table class="table-condensed">
<thead>
<tr>
<th>Company</th>
<th>Contract No</th>
<th>Description</th>
<th>Expires</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@Html.ActionLink(@item.CompanyName, "ViewContract", new { contractID = @item.ID }) </td>
<td width="200">@item.ContractNumber</td>
<td>@item.Description</td>
<td>@Html.DisplayFor(modelItem => item.ExpireDate)<td>
</tr>
}
</tbody> </table>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句