枣粘合剂培养
Date binder culture
我的模型中有 DateTime 字段。我从前端发送日期的格式是 d.m.Y H:i
。并且解析正常。
但是当我将美国日期格式设置为从前端发送并通过在 OnActionExecuting
方法中键入 Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US")
将文化设置为 en-US
时,在我的控制器之前 运行操作它说日期在 if (ModelState.IsValid)
.
无效
我的问题是 Asp.Net 框架中的默认格式设置为 d.m.Y H:i
,我该如何更改该默认格式?活页夹是否考虑了文化,还是总是 d.m.Y H:i
?
我用添加到项目中的 custom data binder 解决了同样的问题。
首先,我添加新的 class DateTimeModelBinder
:
public class DateTimeModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (value != null)
{
DateTime time;
// here you can add your own logic to parse input value to DateTime
//if (DateTime.TryParseExact(value.AttemptedValue, "d.m.Y H:i", CultureInfo.InvariantCulture, DateTimeStyles.None, out time))
if (DateTime.TryParse(value.AttemptedValue, Culture.Ru, DateTimeStyles.None, out time))
{
return time;
}
else
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName,
string.Format("Date {0} is not in the correct format", value.AttemptedValue));
}
}
return base.BindModel(controllerContext, bindingContext);
}
}
然后我在应用程序启动时在 Global.asax.cs
中添加我的 bindeg:
protected void Application_Start(object sender, EventArgs eventArgs)
{
ModelBinders.Binders.Add(typeof(DateTime), new ateTimeModelBinder());
}
感谢 Vadim 提供的解决方案,但我发现了问题并在没有自定义日期绑定器的情况下解决了它。
问题是参数绑定是在我放置 Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US")
的 OnActionExecuting
方法之前完成的,因此当绑定完成时,区域性仍然是默认区域性。默认文化是在Windows(系统语言环境)中设置的文化。
我通过将 <globalization culture="en-US"/>
放在 <system.web>
中 Web.config
来更改它。
现在活页夹可以正确解析美国日期。
我的模型中有 DateTime 字段。我从前端发送日期的格式是 d.m.Y H:i
。并且解析正常。
但是当我将美国日期格式设置为从前端发送并通过在 OnActionExecuting
方法中键入 Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US")
将文化设置为 en-US
时,在我的控制器之前 运行操作它说日期在 if (ModelState.IsValid)
.
我的问题是 Asp.Net 框架中的默认格式设置为 d.m.Y H:i
,我该如何更改该默认格式?活页夹是否考虑了文化,还是总是 d.m.Y H:i
?
我用添加到项目中的 custom data binder 解决了同样的问题。
首先,我添加新的 class DateTimeModelBinder
:
public class DateTimeModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (value != null)
{
DateTime time;
// here you can add your own logic to parse input value to DateTime
//if (DateTime.TryParseExact(value.AttemptedValue, "d.m.Y H:i", CultureInfo.InvariantCulture, DateTimeStyles.None, out time))
if (DateTime.TryParse(value.AttemptedValue, Culture.Ru, DateTimeStyles.None, out time))
{
return time;
}
else
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName,
string.Format("Date {0} is not in the correct format", value.AttemptedValue));
}
}
return base.BindModel(controllerContext, bindingContext);
}
}
然后我在应用程序启动时在 Global.asax.cs
中添加我的 bindeg:
protected void Application_Start(object sender, EventArgs eventArgs)
{
ModelBinders.Binders.Add(typeof(DateTime), new ateTimeModelBinder());
}
感谢 Vadim 提供的解决方案,但我发现了问题并在没有自定义日期绑定器的情况下解决了它。
问题是参数绑定是在我放置 Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US")
的 OnActionExecuting
方法之前完成的,因此当绑定完成时,区域性仍然是默认区域性。默认文化是在Windows(系统语言环境)中设置的文化。
我通过将 <globalization culture="en-US"/>
放在 <system.web>
中 Web.config
来更改它。
现在活页夹可以正确解析美国日期。