c# - 如何为 html 表的日期列提供日期格式 dd/mm/yyyy
问题描述
我正在寻找如何为表格的日期列提供“dd/mm/yyyy”格式
[DataType(DataType.Date, ErrorMessage = "Date only")]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}")]
public string FromDate { get; set; }
<td>@Html.TextBoxFor(model => model.FromDate, new { @class = "form-control date-input emphrs" })</td>
解决方案
你可以这样试试:
模型:
public class DateFormaterModel
{
[DataType(DataType.Date, ErrorMessage = "Date only")]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public string FromDate { get; set; }
[DataType(DataType.Date, ErrorMessage = "Date only")]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public string ToDate { get; set; }
}
控制器:
public IActionResult DateFormatter()
{
return View();
}
看法:
@model MVCApps.Models.DateFormaterModel
@{
ViewData["Title"] = "DateFormatter";
}
<h2>Date Formatter</h2>
<table id="tblEntry" class="table table-striped">
<thead>
<tr>
<th>Date From</th>
<th>Date To</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>@Html.EditorFor(model => model.FromDate, new { @id = "fromDate", htmlAttributes = new { @class = "form-control datepicker w-100"} })</td>
<td>@Html.EditorFor(model => model.ToDate, new { @id = "toDate", htmlAttributes = new { @class = "form-control datepicker w-100" } })</td>
</tr>
</tbody>
</table>
输出:
注意:您错过了
ApplyFormatInEditMode = true
有助于实现这一目标的这个属性。此外,正如@LIama已经说过string
那样,不会在这里采取相应的行动。所以DateTime
将是相同的。但string
也可以按照我向您展示的方式完美运行。
希望它能帮助您解决问题。
推荐阅读
- mysql - SQL 错误 [1248] [42000]:每个派生表都必须有自己的别名
- pine-script - 绘制没有水平线的安全功能?
- javascript - 将 php 数组变量传递给 Javascript 函数时出错
- c# - C#/Unity - 从 UnityWebRequest 协程返回检索到的字符串
- python - Python:我无法获得正确的合并排序
- javascript - Javascript算法 - 按日期列出的消息部分列表
- c++ - 这种连通分量标记算法是新的吗?
- selenium - 是否有任何选项可以处理 chrome 浏览器的证书错误?
- r - 打印没有缺失值的列名
- python - 在 discord.py 中,如何使用 guild.create_role()?