2015-10-04 96 views
0

我正在开发默认文化为en-GB的MVC 5.2应用程序,该应用程序的默认ShortDatePattern为dd/MM/yyyy。我想将日期模式更改为MM/dd/yyyy以查看/编辑模型。以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" }) 

下面是编辑模式下的屏幕截图 - enter image description here

正如你可以看到,默认日期格式在编辑框中显示为dd/MM/yyyy,日期选取器控件不允许我更改i吨。 如何设置日期选择器控件的默认日期模式为MM/dd/yyyy?

仅供参考,我使用

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

谢谢!

回答

1

定义Web.Config中默认文化

<globalization uiCulture="es" culture="es-en" /> 

OR

我们可以进行其他数据类型,如日期时间得到自己的自定义编辑器,只是根据配售的EditorTemplates文件夹的局部视图共享文件夹并将其命名为目标数据类型之后。您将需要自己创建该文件夹,因为它不会自动添加到项目中。

一个DateTime.cshtml可能是这样的......

@model DateTime? 

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

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

@readonly = "readonly"}) 

与jQuery代码这样一个片段到jQuery UI的日期选择器关联的类的DatePicker

<script> 

    $(function() { 

     $(".datePicker").datepicker({ 

      showOn: "button", 

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

      buttonImageOnly: true, 

      buttonText: "Select date" 

     }); 

    }); 

</script> 
+0

的Web.config沿选项不起作用。然后我试着用DateTime EdiotrTemplate,但看起来,它没有被调用。我已经在\ Views \ Shared \ EditorTemplates \ DateTime.cshtml中建议添加DateTime.cshtml – iniki

+0

我通过从ViewModel属性中删除[DataType(DataType.Date)]得到了它。该属性呈现type = date的输入控件。 DateTime的Custom EditorTemplate不适用于类型为“date”的输入控件。 – iniki