Это в основном проблема культуры.Для этого вам нужно написать логику в файле global.asax.cs, как показано ниже.
protected void Application_BeginRequest(Object sender, EventArgs e)
{
CultureInfo newCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
newCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
newCulture.DateTimeFormat.DateSeparator = "/";
Thread.CurrentThread.CurrentCulture = newCulture;
//ModelBinders.Binders.Add(typeof(DateTime), new DateTimeModelBinder());
}
Эта логика используется для установки информации о культуре.Эта же проблема возникнет при публикации даты в формате дд / мм / гггг.Для этого, пожалуйста, напишите логику, как показано ниже, в файле global.asax.cs в разделе «Appication_Start», как показано ниже.
ModelBinders.Binders.Add(typeof(DateTime), new DateTimeModelBinder());
и создайте файл DateTimeModelBinder.cs, как показано ниже: logic
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var holderType = bindingContext.ModelMetadata.ContainerType;
if (bindingContext.ModelMetadata.PropertyName != null)
{
var property = holderType.GetProperty(bindingContext.ModelMetadata.PropertyName);
var displayFormat = string.Empty;
displayFormat = "dd/MM/yyyy";
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (!string.IsNullOrEmpty(displayFormat) && value != null)
{
DateTime date;
displayFormat = displayFormat.Replace("{0:", string.Empty).Replace("}", string.Empty);
// use the format specified in the DisplayFormat attribute to parse the date
if (DateTime.TryParseExact(value.AttemptedValue, displayFormat, 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);
}
Пожалуйстапроверить это.