Using Jquery Globalize with MVC 5

后端 未结 1 1039
终归单人心
终归单人心 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 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; }
    }
    

    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:

    
    
    
    
    
    
    
    
    
    
    
    
    
    

    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)
提交回复
热议问题