我有一个MVC 5项目,
我的项目是多语言的, 我的问题是,当我使用远程验证并发送类型为DateTime的参数时,服务器没有注意它使用默认区域性(“ en-US”)的CurrentCulture。
我的项目中有很多表单,其中大多数都有DateTime对象,并且运行良好。
这是我的代码示例:
查看模型属性
[Display(Name = "CompetitionCycleStartDate", ResourceType = typeof(Competitions))]
[DisplayFormat(DataFormatString = Formats.UKFormat, ApplyFormatInEditMode = true)]
[Required(ErrorMessageResourceName = "CompetitionCycleStartDateRequired", ErrorMessageResourceType = typeof(Competitions))]
[Remote("CheckStartDateCross", "CompetitionCycle", ErrorMessageResourceName = "CycleStartDateCrossMsg", ErrorMessageResourceType = typeof(Competitions))]
public DateTime CompetitionCycleStartDate { get; set; }
远程验证方法
public ActionResult CheckStartDateCross(DateTime CompetitionCycleStartDate)
{
var isItNotCross = competitionCycleService.CheckIfStartDateNotCross(CompetitionCycleStartDate);
return Json(isItNotCross, JsonRequestBehavior.AllowGet);
}
设置区域性代码
protected override void Initialize(RequestContext requestContext)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
var routeData = requestContext.RouteData;
if (routeData.Values.ContainsKey("MS_DirectRouteMatches"))
{
routeData = ((IEnumerable<RouteData>)routeData.Values["MS_DirectRouteMatches"]).First();
CurrentLanguageCode = routeData?.Values["lang"] as string;
}
else if (requestContext.RouteData.Values["lang"] != null && requestContext.RouteData.Values["lang"] as string != "null")
{
CurrentLanguageCode = (string)routeData.Values["lang"];
}
if (CurrentLanguageCode != null)
{
try
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(CurrentLanguageCode);
}
catch (Exception)
{
throw new NotSupportedException($"Invalid language code '{CurrentLanguageCode}'.");
}
}
else
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar");
}
base.Initialize(requestContext);
}
Web.Config
<globalization culture="en-GB"></globalization>