2012-05-02 7 views
3

私はコードを持っています。MVC 3でdatetime形式を変更する方法

モデル

[Required(ErrorMessage = "Bạn hãy nhập ngày tháng năm sinh")] 
    [DataType(DataType.Date, ErrorMessage = "Ngày tháng năm sinh không chính xác, bạn hãy nhập lại")] 
    [ValidationHelper.DateFormat(ErrorMessage = "Ngày tháng năm sinh không chính xác, bạn hãy nhập lại")] 
    [DisplayFormat(DataFormatString = "{MM/dd/yyyy}")] 
    [Display(Name = "Ngày sinh")] 
    public System.DateTime DateOfBirth { get; set; } 

ビュー

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 


@using (Html.BeginForm((string)ViewBag.FormAction, "Account")) 
{ 
    <fieldset> 
     <legend>Registration Form</legend> 
     <ol> 

      <li> 
       @Html.LabelFor(m => m.DateOfBirth) 
       @Html.EditorFor(m => m.DateOfBirth) 
       @Html.ValidationMessageFor(m => m.DateOfBirth) 
      </li> 
     </ol> 
    </fieldset> 
} 

私は入力2012年5月31日=>値'05/31/2012' CMTキャップNgàyのために有効ではありません。 私は入力するとき31/05/2012 =>フィールドは、日付でなければなりません。 何が起こったのですか?私はベトナム出身です。私の英語はとても悪いですが、私を助けてください!どうもありがとう! 私のローカルの日付書式yyyy/MM/DD

答えて

2

で試してみてください:

@item.Date.ToString("dd MMM yyyy") 

か、あなたのビューモデルの[DisplayFormat]属性使用することができます単に

[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")] 
pubilc DateTime Date { get; set } 

とあなたのビュー内を:

@Html.DisplayFor(x => x.Date) 
0

お使いの日付の表示形式は次のようです:

[DisplayFormat(DataFormatString = "{MM/dd/yyyy}")] 

あなたが31/05/2012の日付になりたい場合は、あなたがあなたの表示形式を変更することができます:DisplayFormatEditorForで動作するように取得するには

[DisplayFormat(DataFormatString = "{dd/MM/yyyy}")] 
2

、あなたはApplyFormatInEditModeを追加する必要があります次のようになります。

[DisplayFormat(DataFormatString="{0:MM/dd/yyyy}", ApplyFormatInEditMode=true)]

関連する問題