我試圖處理用戶輸入的日期值,我要提示用戶輸入日期格式爲:DD/MM/YYYY指定MVC5(DD/MM/YYYY)日期格式
什麼我試圖做的:
我讀貫徹答案達林這樣一個問題: Format datetime in asp.net mvc 4
這是我實現:
在Global.asax中
在示範(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var displayFormat = bindingContext.ModelMetadata.DisplayFormatString;
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (!string.IsNullOrEmpty(displayFormat) && value != null)
{
DateTime date;
displayFormat = displayFormat.Replace
("{0:", string.Empty).Replace("}", string.Empty);
if (DateTime.TryParse(value.AttemptedValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
return date;
}
else
{
bindingContext.ModelState.AddModelError(
bindingContext.ModelName,
string.Format("{0} is an invalid date format", value.AttemptedValue)
);
}
}
return base.BindModel(controllerContext, bindingContext);
}
出發日期:
[Required(ErrorMessage = "Departure date is required")]
[Display(Name = "Departure Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime DepartureDate { get; set; }
鑑於:
<div class="col span-1-of-2">
@Html.LabelFor(m => m.DesignVacation.DepartureDate)
@Html.EditorFor(m => m.DesignVacation.DepartureDate)
</div>
它始於本月(毫米)但我想要的是這種格式: dd/MM/yyyy
如何使用editorFor獲取此格式(如果可能則使用textboxFor)。