1

我正在尝试更改DateFormat2021-02-18T09:58在 DateTime 字段中添加我的模型

 [DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}")]
        public DateTime DateAndTime { get; set; } = DateTime.Now;

同样在我的Create.cshtml视图中我添加asp-format="{0:yyyy-MM-ddTHH:mm}"但仍然有问题。由于我使用数据表,因此我也尝试在客户端渲染此日期格式,但不起作用。有人能告诉我我在哪里犯错了吗?这个 DataAnnotation 有什么问题?

var dataTable;

$(document).ready(function () {
    loadDataTable();
});


function loadDataTable() {
    dataTable = $('#tblData').DataTable({
        "ajax": {
            "url": "/Manager/Ticket/GetAll"
        },
        "columns": [
            { "data": "title", "width": "15%" },
            { "data": "description", "width": "15%" },
            { "data": "dateAndTime", "width": "15%", "dateFormat": "mm/dd/yy" },
            {
                "data": "id",
                "render": function (data) {
                    return `
                            <div class="text-center">
                                <a href="/Manager/Ticket/Upsert/${data}" class="btn btn-success text-white" style="cursor:pointer">
                                    <i class="fas fa-edit"></i> 
                                </a>
                                <a onclick=Delete("/Manager/Ticket/Delete/${data}") class="btn btn-danger text-white" style="cursor:pointer">
                                    <i class="fas fa-trash-alt"></i> 
                                </a>
                            </div>
                           `;
                }, "width": "25%"
            }
        ]
    });
}


function Delete(url) {
    swal({
        title: "Are you sure you want to Delete?",
        text: "You will not be able to restore the data!",
        icon: "warning",
        buttons: true,
        dangerMode: true
    }).then((willDelete) => {
        if (willDelete) {
            $.ajax({
                type: "DELETE",
                url: url,
                success: function (data) {
                    if (data.success) {
                        toastr.success(data.message);
                        dataTable.ajax.reload();
                    }
                    else {
                        toastr.error(data.message);
                    }
                }
            });
        }
    });
}

同样在我的IndexPage地方 DateTime 应该显示为 YYYY-MM-DDTHH:mm

@model VmSTicketing.Models.Ticket
@{
    ViewData["Title"] = "Index";
}

<br />
<div class="row">
    <div class="col-6">
        <h2 class="text-primary">Lista svih tiketa</h2>
    </div>
    <div class="col-6 text-right">
        <a class="btn btn-primary" asp-action="Upsert"><i class="fas fa-plus"></i> &nbsp; Novi tiket</a>
    </div>
</div>

<br />
<div class="p-4 border rounded">
    <table id="tblData" class="table table-striped table-bordered" style="width:100%">
        <thead class="thead-dark">
            <tr class="table-info">
                <th>Title</th>
                <th>Description</th>
                <th>DateAndTime</th>
                <th></th>
            </tr>
        </thead>
    </table>
</div>

@section Scripts{
    <script src="~/js/ticket.js"></script>
}
4

0 回答 0