使用Jquery Globalize和MVC 5

我试图在MVC5中使用带有jquery globalize插件的MVC unobstrusivevalidation(与jquery-validate-globalize包一起使用)。 出于学习目的,我按照此处启动了一个演示项目,但它无法使用globalize运行(它适用于默认的Microsoft unobstrusivevalidation)。 该模型非常简单:

public class GlobalizeModel { [Range(10.5D, 20.3D)] public decimal Double { get; set; } [Required] public DateTime? DateTime { get; set; } } 

我尝试在_Layout页面底部启动Globalize,如下所示(视图最小,只有2个输入):(我从https://johnnyreilly.github.io/globalize-so-what-c​​ha获取必要文件列表- 想要/ )

               $.when( $.getJSON("/Scripts/cldr/supplemental/likelySubtags.json"), $.getJSON("/Scripts/cldr/main/en/numbers.json"), $.getJSON("/Scripts/cldr/supplemental/numberingSystems.json"), $.getJSON("/Scripts/cldr/main/en/ca-gregorian.json"), $.getJSON("/Scripts/cldr/main/en/timeZoneNames.json"), $.getJSON("/Scripts/cldr/supplemental/timeData.json"), $.getJSON("/Scripts/cldr/supplemental/weekData.json"), $.getJSON("/Scripts/cldr/main/tr/numbers.json"), $.getJSON("/Scripts/cldr/main/tr/ca-gregorian.json"), $.getJSON("/Scripts/cldr/main/tr/timeZoneNames.json"), console.log("JSONs loaded") ).then(function () { console.log("start slicing"); return [].slice.apply(arguments, [0]).map(function (result) { console.log("slicing done"); return result[0]; }); }).then(Globalize.load).then(function () { Globalize.locale("en"); console.log("Locale set to en"); }).then(console.log("LOADED EVERYTHING"));  

但是当我运行页面时,我只看到控制台记录JSOns loadedJSOns loaded LOADED EVERYTHING 。 此外,当我通过在数字文本框中键入任何内容来尝试客户端validation时(当然当焦点丢失时),我在控制台中收到以下错误:

 Uncaught Error: E_DEFAULT_LOCALE_NOT_DEFINED: Default locale has not been defined. 

这篇文章是类似的,我试着检查那里列出的东西。 我认为我的JSON对象没有被提取,但我不是一个JS,所以我不确定。 我在web.config中添加了以下项目,看看这是否与文件服务有关,但没有用:

       

文档在web.config中设置为auto,如下所示:

      

你可以在这里看到Scripts文件夹结构:

<img src="http://sofzh.miximages.com/javascript/KzVfB.png" alt="您可以在此处查看 Scripts 文件夹结构”>

那么,这里的问题是什么? 我怎么能让这个东西工作?

我最近遇到了同样的问题,试图将I18n添加到MVC5网络应用程序中。 经过几天的研究并部分使用您的代码作为基础,我找到了一些帮助我实现它的东西。

我的解决方案:在一个单独的项目中,我将十进制和DateTime属性添加到ApplicationUser类:

 public class ApplicationUser : IdentityUser { public async Task GenerateUserIdentityAsync(UserManager manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here return userIdentity; } public DateTime birthdate { get; set; } public decimal balance { get; set; } } 

我还修改了RegisterViewModel以接受这些属性,如下所示:

 public class RegisterViewModel { [Required] [EmailAddress] [Display(Name = "Email")] public string Email { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirm password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } [Required] [DataType(DataType.DateTime)] public DateTime birthdate { get; set; } [Required] [DataType(DataType.Currency)] public decimal balance { get; set; } } 

然后,我将文化设置在基本控制器中,其他控制器从该控制器inheritance:

 public class BaseController : Controller { protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state) { string[] cultures = { "es-CL", "es-GT", "en-US" }; Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultures[1]); Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; return base.BeginExecuteCore(callback, state); } } 

这只是出于测试目的,而不是我在真实应用中获取文化的方式。

我的文件结构与你的相同,我没有修改web.config文件。

我还使用此链接进行依赖。 但后来我在Register.cshtml的scripts部分修改了一些东西:

              

_Layout视图脚本根本没有修改,我对控制台日志没有任何问题。

就这一切而言,它对我有用,而且这是一个非常相似的案例,我希望它对你也有用。