【问题标题】:How can show the EditorFor model value with date format如何以日期格式显示 EditorFor 模型值
【发布时间】:2021-10-19 12:23:24
【问题描述】:

我有一个 html 页面的给定模型来接受日期。我正在尝试将 datefrom 和 dateto 输入框格式化为 "dd/mm/yyyy" 格式。目前,它仅显示为文本格式。如何让两列都接受数据为 dd/mm/yyyy。

EditDateModel
public class EditDateModel
    {
        public string FromDate { get; set; }
        public string ToDate { get; set; }
        
    }

@model EditDateModel
<table id="tblEntry" class="table table-striped">
    <thead>
        <tr>
            <th>Date From</th>
            <th>Date To</th>
        </tr>
    </thead>
    <tbody>
 <tr>
            <td>@Html.EditorFor(model => model.FromDate, new { htmlAttributes = new { @class = "form-control datepicker w-100" } })</td>
            <td>@Html.EditorFor(model => model.ToDate, new { htmlAttributes = new { @class = "form-control w-100" } })</td>
  </tr>

    </tbody>
</table>

【问题讨论】:

标签: c# asp.net-core razor-pages


【解决方案1】:

您不需要使用任何其他库。您可以使用asp.net DataAnnotations 来完成它的推荐,方便且易于使用。

Implementation:

让你的EditDateModel 如下所示:

public class EditDateModel
    {
        [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; }
    }

Note: 它需要using System.ComponentModel.DataAnnotations; 在顶部的包引用中引用。

Views:

<table id="tblEntry" class="table table-striped">
    <thead>
        <tr>
            <th>Date From</th>
            <th>Date To</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>@Html.EditorFor(model => model.FromDate, new { htmlAttributes = new { @class = "form-control datepicker w-100" } })</td>
            <td>@Html.EditorFor(model => model.ToDate, new { htmlAttributes = new { @class = "form-control datepicker w-100" } })</td>
        </tr>

    </tbody>
</table>

注意:您的FromDate 的格式是正确的

OutPut:

如果您想获得更多知识,可以参考我们的official documet here。对于任何类型的日期格式问题,您也可以refer this

希望它能解决您的问题。

【讨论】:

    【解决方案2】:

    你在正确的轨道上,但这里的事情是...... asp.net mvc 只负责生成 HTML 资源,以及嵌入其中的所有脚本。 因此,您将始终获得文本值。

    为了满足您的要求,我建议在 js 中使用第三个支持转换实际 ui 交互日期格式的库。例如这里,我将使用 jquery 和 datepicker

    // This is up at your HTML
    <td>@Html.EditorFor(model => model.FromDate, new { htmlAttributes = new { @class = "form-control datepicker w-100" } })</td>
    
    // And this should be on your js below that
    $(document).ready(() => {
                $('.datepicker').datepicker({
                    format: "dd/mm/yyyy"
                });
            });
    // Please don't forget to embed the library somewhere
    

    【讨论】:

    • 我不认为jquery 是必需的,无论@Alan Pauil 要实现什么。所以不能同意你的解决方案。
    • @MdFaridUddinKiron 你是对的...... Jquery 绝对不是 Alan 想要的,但是可以考虑使用 datepicker 库来适应未来的定制。这就是为什么我只推荐lib。这实际上不是必须的
    • 如果可以使用 asp.net core 内置功能对其进行管理,为什么要使用 jquery 他的问题只需要日期时间功能,但为此他需要使用完整的 jquery 库,这根本不优雅.如果他已经在客户端使用了`jquery`,那么你会建议他这样做吗?
    • @MdFaridUddinKiron 你发现他在&lt;td&gt; 标签上的类名有datepicker 吗?这就是为什么我假设并推荐他的要求。我不相信他可以凭空取这个班级名称
    • 我已经为他提供了保留所有现有代码的解决方案。
    猜你喜欢
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 2022-07-29
    相关资源
    最近更新 更多