我将 MVC3 解决方案升级到 MVC4。迁移后,验证器被破坏。
如果我选择德语作为语言,我的输入日期是“2013年3月20日”。我在 MVC4 中收到验证错误,但在 MVC3 中却没有。如果我将格式从“20.03.2013”替换为“20/03/2013”,它可以在 MVC4 中工作,但不能在 MVC3 中工作;-)
I set the UI culture of the current thread to german. The output of the ResX values are in the correct language, so i know there should be no error with the culture., only for the site itself. The error messages are in english, but the site is in german.
我认为这意味着验证器使用了错误的 UI 文化。
这是我使用的代码。
[Required(ErrorMessageResourceName = "Error_DepartureDate", ErrorMessageResourceType = typeof(Resx.Query))]
public DateTime? DepartureDate { get; set; }
我认为默认模型绑定程序有问题,因为渲染的 html 看起来不错:
data-lang="de" data-mindate="3" data-val="true" data-val-required="Bitte geben Sie das gewünschte Reisedatum des Hinflugs ein." id="DepartureDate" name="DepartureDate" tabindex="3" type="text" value=""
我将 Jscript 升级到使用 Visual Studio 2012(已安装 SP1)模板创建新的 Mvc 应用程序时附带的源。这没有影响。
我有一个 CultureModelBinder,它从会话中读取当前的文化,并使用一个小的辅助函数设置文化。
public static void UpdateThreadCulture(CultureInfo culture)
{
Thread.CurrentThread.CurrentUICulture = culture;
}
文化模型绑定器是默认绑定器。
ModelBinders.Binders.DefaultBinder = new CultureModelBinder();
ModelBinders.Binders.Add(typeof(DateTime?), new DateTimeModelBinder());
// and many, many more
也许 mvc4 的执行顺序发生了变化,导致了问题?
更新:该项目使用 .NET Framework 4.5 作为目标。
更新2:
我有一个组合框,用户可以在其中选择 16 种不同的语言,每种语言可能有其自己的特定格式。
例如。
DE-de -> DD.MM.YYYY;
zh-cn -> 日/月/年;
en-us -> 月/日/年
我刚刚得到关于设置当前文化的提示,这是它应该正确的证据。当验证器失败时,此代码不会被命中,看起来它发生在客户端。
public class DateTimeModelBinder : IModelBinder
{
private LogService _log = new LogService();
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
object result = null;
ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (valueResult != null)
{
try
{
var stateHandler = new StateHandler(controllerContext.HttpContext.Session);
result = valueResult.ConvertTo(typeof(DateTime?), stateHandler.Culture);
}
catch
{
try
{
result = valueResult.ConvertTo(typeof(DateTime?), CultureInfo.InvariantCulture);
}
catch (Exception ex)
{
_log.Error("DateTimeModelBinder parse exception", ex);
_log.KeyValue("AttemptedValue", valueResult.AttemptedValue);
}
}
}
return result;
}
}
为了完整起见,我的文化模型活页夹:
public class CultureModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
StateHandler stateHandler = new StateHandler(controllerContext.HttpContext.Session);
Helper.UpdateThreadCulture(stateHandler.Culture);
return base.BindModel(controllerContext, bindingContext);
}
}
更新:也许与这个问题有关联:http://connect.microsoft.com/VisualStudio/feedback/details/705643/a-data-val-date-attribute-is- generated-for-time-fields-in-asp-net-mvc-4 http://connect.microsoft.com/VisualStudio/feedback/details/705643/a-data-val-date-attribute-is-generated-for-time-fields-in-asp-net-mvc-4
更新:
阅读以下文章:http://weblogs.asp.net/scottgu/archive/2010/06/10/jquery-globalization-plugin-from-microsoft.aspx http://weblogs.asp.net/scottgu/archive/2010/06/10/jquery-globalization-plugin-from-microsoft.aspx
尝试了以下方法:
按以下顺序加载脚本:
/Scripts/jquery-1.8.3.min.js
/Scripts/globalize.js
/Scripts/cultures/globalize.cultures.js
// and much more other scripts...
添加了通话。输出正确为“DE”。
var currentLanguage = $("#DepartureDate").attr("data-lang");
alert(currentLanguage);
$.preferCulture(currentLanguage);
对验证者没有影响...