DateTime格式调试 [英] DateTime format debugging

查看:90
本文介绍了DateTime格式调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在努力使用DateTime格式.我正在制作一个ASP.NET MVC应用程序,并且具有DateTime类型的DateOfBooking属性.发送表单时,此属性将预订DateTime.Date.Now.

So I have been struggling with DateTime formats. I am making an ASP.NET MVC app and I have a DateOfBooking property which is DateTime type. This property books DateTime.Date.Now when the form is sent.

下面是我的媒体资源和DataAnotations,用于格式化我的日期.我需要的格式是dd/MM/yyyy,但是我在DB(MS SQL Server Managment Studio)中的列的格式是yyyy-MM-dd.

Below is my property and DataAnotations used to format my date. Format which i need is dd/MM/yyyy, but my column in DB (MS SQL Server Managment Studio) is in format yyyy-MM-dd.

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime DateOfBooking { get; set; }

无论如何,上面的代码使我能够在Html.EditorFor helper(在MVC模板视图的Edit View内)中获取DateOfBooking的值,因为没有它,我的助手只会显示(带有日期选择器)dd/MM/yyyy现在它显示了从数据库中提取的实际值.

Anyway, code above enabled me to get a value of my DateOfBooking inside Html.EditorFor helper (inside Edit View which is an MVC templated view) because without it my helper would just show (with date picker) dd/MM/yyyy and now it shows an actual value which is pulled from the DB.

现在,由于我的索引页面上方的那行代码(这是预订列表,以yyyy-MM-dd格式显示日期),这就是我遇到的问题.幸运的是一个小时左右后,我找到了解决方案.

Now because of that line of code above my Index page which is a list of bookings showed date in format yyyy-MM-dd and this is where I got stuck. Luckily after an hour or so i found a solution.

 @foreach (var item in Model) 
    {
    var date = item.DateOfBooking.ToString("dd/MM/yyyy");
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Course.CourseName)
        </td>
        <td> 
            @Html.DisplayFor(modelItem => date)
        </td>

添加 var date 后,一切都按照我想要的方式工作.但是我不明白为什么我不能像这样去: @ Html.DisplayFor(modelItem => item.DateOfBooking.ToString("dd/MM/yyyy"))

After adding var date everything works just the way I want it. But I dont get it why couldnt I just go like this: @Html.DisplayFor(modelItem => item.DateOfBooking.ToString("dd/MM/yyyy"))

我想知道为什么没有 [DisplayFormat(DataFormatString ="{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)]的EditorHelper不能正确显示我的日期的原因是什么?添加 var date ?

I want to know why couldnt EditorHelper show my date properly without [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] and what difference was made by adding var date?

推荐答案

Html.DisplayFor()具有模板系统,因此您可以定义数据在整个应用程序中的显示方式.

Html.DisplayFor() has a templating system so that you can define how your data should appear throughout your application.

您可以在 Views/Shared/DisplayTemplates/DateTime.cshtml 中创建一个模板,该模板将在调用DisplayFor时格式化所有DateTime属性.例如

You can create a template in Views/Shared/DisplayTemplates/DateTime.cshtml that will format all DateTime properties when DisplayFor is called. e.g.

 // DateTime.cshtml
 @model System.DateTime
 @Model.ToString("dd/MM/yyyy")

更多信息 https://exceptionnotfound.net/asp-net-mvc-demystified-display-and-editor-templates/

另一种方法是不使用DisplayFor而是直接输出字符串,例如

Another way to do it is to just not use DisplayFor and just output the string directly e.g.

@foreach (var item in Model) 
    {
    <tr>
        <td>
            @item.DateOfBooking.ToString("dd/MM/yyyy");
        </td>

这对于一个或两个字段来说很好,但是如果要一致地设置所有日期的格式,则最好使用模板系统.

This is fine for one or two fields but if all dates are to be formatted consistently it is better to use the template system.

这篇关于DateTime格式调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆