2015-10-04 94 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