Chrome 中 MVC5 中的编辑日期未显示正确格式
Date in Editing in MVC5 in Chrome does not show correct format
刚开始做 MVC。按照本页中的示例: Adding Validation in this Getting Started with ASP.NET MVC 5 tutorial.
在模型中,有一个日期和格式为:
using System;
using System.ComponentModel.DataAnnotations;
namespace MvcMovie.Models
{
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
[Display(Name = "Release Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
}
日期格式根据SO Answer。因此(使用 Chorme、Edge 和 IE)当我在 i) 索引页面、ii) 详细信息页面,甚至在 iii) 删除页面中,我看到日期显示为 yyyy-MM-dd(例如 1959-04-15 1959 年 4 月 15 日)。
但是当我进入编辑页面时,不同浏览器的日期是不同的。
Chrome:1959 年 4 月 15 日
边缘:1959 年 4 月 15 日
即:1959-04-15
(当我在所有3个浏览器中查看源代码时,都显示1959-04-15。)
我认为以下建议(根据 the tutorial's description 引起了麻烦,但即使我删除了以下文化规范,所有浏览器的结果都是相同的。
<system.web>
<globalization culture ="en-US" />
<!--elements removed for clarity-->
</system.web>
- 我电脑的区域设置是'English (Hong Kong SAR)'。短日期的格式为 d/M/yyyy。
- 另外,当我查看本地数据库中的数据时,显示的日期是“15/4/1959 0:00:00”。看起来它使用了计算机的区域设置。
谁能解释为什么编辑模式下的日期不同?还有如何修复 Chrome?我知道有人建议了其他方法(比如添加模型、文件等),但我只是想看看是否有不添加额外内容的简单解决方案 file/model。谢谢。
[DataType(DataType.Date)]
在使用 @Html.EditorFor()
时生成 type="date"
属性,后者又生成浏览器 HTML-5 日期选择器。 type="date"
在 IE 中不受支持,这就是为什么您得到的文本框仅包含以指定格式显示的日期。
有关 HTML-5 跨各种浏览器的支持比较,请参阅 HTML 5 TEST。另请注意,FireFox 才刚刚开始支持 type="date"
.
至于在您的 Index
和 Details
视图中以 yyyy-MM-dd
格式显示的日期,那是因为您使用的 @Html.DisplayFor()
使用 DataFormatString
属性 of [DisplayFormat]
来格式化值。
您的 Index
和 Details
视图中的选项是仅使用
@Model.YourDate.ToString("d") // or what ever format string you want
另一种方法是将属性更改为
DisplayFormat(DataFormatString = "{0:d}")]
以便@Html.DisplayFor()
生成正确的格式,然后在Edit
视图中,使用
@Html.TextBoxFor(m => m.YourDate, "{0:yyyy-MM-dd}", new { type = "date" })
生成浏览器 HTML-5 日期选择器。
但是如果你想要一个跨所有浏览器的一致的日期选择器,那么你需要使用一个 jQuery 插件(例如 jQuery UI)。
另请注意,服务器上的文化与浏览器无关HTML-5 datepicker,它以浏览器文化呈现日期。
刚开始做 MVC。按照本页中的示例: Adding Validation in this Getting Started with ASP.NET MVC 5 tutorial.
在模型中,有一个日期和格式为:
using System;
using System.ComponentModel.DataAnnotations;
namespace MvcMovie.Models
{
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
[Display(Name = "Release Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
}
日期格式根据SO Answer。因此(使用 Chorme、Edge 和 IE)当我在 i) 索引页面、ii) 详细信息页面,甚至在 iii) 删除页面中,我看到日期显示为 yyyy-MM-dd(例如 1959-04-15 1959 年 4 月 15 日)。
但是当我进入编辑页面时,不同浏览器的日期是不同的。
Chrome:1959 年 4 月 15 日
边缘:1959 年 4 月 15 日
即:1959-04-15
(当我在所有3个浏览器中查看源代码时,都显示1959-04-15。)
我认为以下建议(根据 the tutorial's description 引起了麻烦,但即使我删除了以下文化规范,所有浏览器的结果都是相同的。
<system.web>
<globalization culture ="en-US" />
<!--elements removed for clarity-->
</system.web>
- 我电脑的区域设置是'English (Hong Kong SAR)'。短日期的格式为 d/M/yyyy。
- 另外,当我查看本地数据库中的数据时,显示的日期是“15/4/1959 0:00:00”。看起来它使用了计算机的区域设置。
谁能解释为什么编辑模式下的日期不同?还有如何修复 Chrome?我知道有人建议了其他方法(比如添加模型、文件等),但我只是想看看是否有不添加额外内容的简单解决方案 file/model。谢谢。
[DataType(DataType.Date)]
在使用 @Html.EditorFor()
时生成 type="date"
属性,后者又生成浏览器 HTML-5 日期选择器。 type="date"
在 IE 中不受支持,这就是为什么您得到的文本框仅包含以指定格式显示的日期。
有关 HTML-5 跨各种浏览器的支持比较,请参阅 HTML 5 TEST。另请注意,FireFox 才刚刚开始支持 type="date"
.
至于在您的 Index
和 Details
视图中以 yyyy-MM-dd
格式显示的日期,那是因为您使用的 @Html.DisplayFor()
使用 DataFormatString
属性 of [DisplayFormat]
来格式化值。
您的 Index
和 Details
视图中的选项是仅使用
@Model.YourDate.ToString("d") // or what ever format string you want
另一种方法是将属性更改为
DisplayFormat(DataFormatString = "{0:d}")]
以便@Html.DisplayFor()
生成正确的格式,然后在Edit
视图中,使用
@Html.TextBoxFor(m => m.YourDate, "{0:yyyy-MM-dd}", new { type = "date" })
生成浏览器 HTML-5 日期选择器。
但是如果你想要一个跨所有浏览器的一致的日期选择器,那么你需要使用一个 jQuery 插件(例如 jQuery UI)。
另请注意,服务器上的文化与浏览器无关HTML-5 datepicker,它以浏览器文化呈现日期。