以 MM/dd/yyyy 格式显示日期

Display date in MM/dd/yyyy format

我正在开发默认文化为 en-GB 的 MVC 5.2 应用程序,其默认 ShortDatePattern 为 dd/MM/yyyy。我想将 view/edit 型号的日期模式更改为 MM/dd/yyyy。

显示模型时工作正常。但是在编辑模式下显示日期时会遇到问题。下面是我的代码 -
视图模型

[Display(Name="License Renewal Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString="{0:MM/dd/yyyy}", ApplyFormatInEditMode=true)]
public DateTime LicenseRenewalDate { get; set; }

.cshtml

@Html.EditorFor(model => model.LicenseRenewalDate, new { htmlAttributes = new { @class="form-control" } })
@Html.ValidationMessageFor(model => model.LicenseRenewalDate, "", new { @class="text-danger" })

下面是编辑模式的屏幕截图 -

如您所见,默认日期模式在编辑框中显示为 dd/MM/yyyy,并且日期选择器控件不允许我更改它。 如何将日期选择器控件设置为默认日期模式 MM/dd/yyyy?

仅供参考,我正在使用

jQuery.Validation version - 1.11.1 and 
Microsoft.jQuery.Unobtrusive.Validation version - 3.2.3

谢谢!

在 Web.Config

中定义默认文化
<globalization uiCulture="es" culture="es-en" />

我们可以让DateTime等其他数据类型拥有自己的自定义编辑器,只需在Shared文件夹下的EditorTemplates文件夹中放置一个部分View,并以目标数据类型命名即可。您需要自己创建此文件夹,因为它不会自动添加到项目中。

A DateTime.cshtml 可能看起来像这样……

@model  DateTime?

@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : 

string.Empty),new { @class = "datePicker" , 

@readonly = "readonly"})

连同这样的 jQuery 代码片段,将 jQuery UI 日期选择器关联到 class datePicker

<script>

     $(function () {

         $(".datePicker").datepicker({

            showOn: "button",

             buttonImage:  "/images/calendar-icon.png",

             buttonImageOnly: true,

            buttonText: "Select date"

         });

     });

</script>