【问题标题】:DataTables render wrong DateTime value from databaseDataTables 从数据库中呈现错误的 DateTime 值
【发布时间】:2019-09-24 10:52:49
【问题描述】:

我在 asp.net mvc 项目中使用数据表插件,在绑定数据时它呈现错误的日期时间值

数据库中的实际日期时间值为 05-May-19 2:43:33 PM ,运行时显示的值为 /Date(1557060213477)/

它显示数据库中存在的日期时间值的预期结果

这是我的模型

 public int Id { get; set; }
 [Required]
 public string Name { get; set; }
 Display(Name ="Upload File")]
 public string FileUrl { get; set; }
 public DateTime AddedOn { get; set; }

这是我的看法

<table id="DDR" class="table table-striped table-bordered dt-responsive nowrap">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Uploaded File</th>
            <th>Uploaded At</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>
    </thead>
</table>
@section scripts {
    <link href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css" rel="stylesheet" />
    <script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
    <script>
        $(document).ready(function () {
            debugger;
            $("#DDR").DataTable({
                "ajax": {
                    "url": "/DomesticDocRepositories/GetData",
                    "type": "GET",
                    "datatype": "json"
                },
                "columns": [
                    {
                        "data": "Id",
                        render: function (data, type) {
                            return '<a href="/DomesticDocRepositories/Details/' + data + '">'+data+"</a>";
                        }
                    },

                    { "data": "Name" },
                    {
                        data: "FileUrl",
                        render: function (data, type) {
                            return "<a href='/DomesticFiles/" + data + " 'target='_blank''>" + data + "</a>";
                        }
                    },
                    {
                        "data": "AddedOn",
                        render: function (data, type) {
                            return '<span "title='+ data +"'>" + data +"</span>";
                        }
                    },
                    {
                        "render": function (data, type, full, meta) { return '<a class="btn btn-info" href="/DomesticDocRepositories/Edit/' + full.Id + '">Edit</a>'; }
                    },
                    {
                        data: null, render: function (data, type, row) {
                            return '<a class="btn btn-danger" href="/DomesticDocRepositories/Delete/' + row.Id + '">Delete</a>';
                        }
                    },
                ]
            });
        });
    </script>
    }

【问题讨论】:

    标签: javascript c# jquery asp.net-mvc datatables


    【解决方案1】:

    使用moment.js 表示日期时间(docs)。在您的渲染中,您应该在渲染中使用:

    {
        render: function (data, type, row) {//data
            return moment(row.AddedOn ).format('DD/MM/YYYY hh:mm:ss');//or another format you wantt.
        }
    }
    

    【讨论】:

    • Uncaught ReferenceError: moment is not defined @hassan
    • 对不起,我忘了写moment.js,我编辑我的答案。
    猜你喜欢
    • 1970-01-01
    • 2017-06-18
    • 2021-06-30
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    相关资源
    最近更新 更多