Reputation: 41
I have code.
Model
[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; }
View
<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>
}
when i input 05/31/2012 => The value '05/31/2012' is not valid for Ngày cấp CMT. when i input 31/05/2012 =>The field Ngày cấp CMT must be a date. What happened is that? I am from Viet Nam. My English very bad but please help me! Thanks very much! My local date format yyyy/MM/dd
Upvotes: 3
Views: 2923
Reputation: 12321
To get the DisplayFormat
to work with EditorFor
, you need to add the ApplyFormatInEditMode
as follows:
[DisplayFormat(DataFormatString="{0:MM/dd/yyyy}", ApplyFormatInEditMode=true)]
Upvotes: 2
Reputation: 27934
It seems have a Display Format for your dates:
[DisplayFormat(DataFormatString = "{MM/dd/yyyy}")]
if you want 31/05/2012 to be a date, you can change your Display Format to:
[DisplayFormat(DataFormatString = "{dd/MM/yyyy}")]
Upvotes: 0
Reputation: 6500
Try with:
@item.Date.ToString("dd MMM yyyy")
or you could use the [DisplayFormat]
attribute on your view model:
[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
pubilc DateTime Date { get; set }
and in your view simply:
@Html.DisplayFor(x => x.Date)
Upvotes: 2