Using Jquery Globalize with MVC 5

后端 未结 1 1038
终归单人心
终归单人心 2021-01-15 08:02

I am trying to use the MVC unobstrusive validation with jquery globalize plugin in MVC5 (in conjunction with the package jquery-validate-globalize). For learning purposes I

相关标签:
1条回答
  • 2021-01-15 08:36

    I recently ran into the same problem, trying to add I18n to an MVC5 web app. After several days of research and partly using your code as base, I found some things that helped me implement it.

    My Solution: In a separate project, I added decimal and DateTime properties to the ApplicationUser class:

    public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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; }
    }
    

    I also modified the RegisterViewModel to accept those properties, as follows:

    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; }
    }
    

    Then, I set the culture in a base controller, from which other controllers inherit:

    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);
        }
    }
    

    That's just for testing purposes, not the way I fetch culture in the real app.

    My file structure is the same as yours and I didn't modify the web.config file.

    I also used this link for dependencies. But then I modified a few things in the scripts section of Register.cshtml:

    <!-- CLDR -->
    <script src="~/Scripts/cldr.js"></script>
    <script src="~/Scripts/cldr/event.js"></script>
    <script src="~/Scripts/cldr/supplemental.js"></script>
    <!-- Globalize -->
    <script src="~/Scripts/globalize.js"></script>
    <script src="~/Scripts/globalize/number.js"></script>
    <script src="~/Scripts/globalize/date.js"></script>
    <!-- $ validate -->
    <script src="~/Scripts/jquery.validate.js"></script>
    <script src="~/Scripts/jquery.validate.globalize.js"></script>
    <!-- fetch files -->
    <script>
        $.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"),
            ).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"));
    </script>
    

    The _Layout view scripts weren't modified at all, and I had no problem with the console logs.

    That's all, it worked out for me, and as it's a very similar case, I hope it works for you too.

    0 讨论(0)
提交回复
热议问题