How to translate Identity Password validation messages

后端 未结 3 1881
抹茶落季
抹茶落季 2021-01-04 12:04

So far I have been able to translate everything in an ASP.Net Core 2.1 Web Application.

It has proven a little challenge, since the scaffolded Account Pages needed a

3条回答
  •  被撕碎了的回忆
    2021-01-04 12:31

    You can use ViewModel and than you can just use DataAnnotations here is how Controller, ViewModel and View looks in that case (translation is Croatian).

    Controller

        [HttpGet]
        public IActionResult Login()
        {
            return View();
        }
    
        [HttpPost]
        public async Task Login(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await signInManager.PasswordSignInAsync(
                    model.Email, model.Password, model.RememberMe, false);
    
                if (result.Succeeded)
                {
                    return RedirectToAction("index", "home");
                }
    
                ModelState.AddModelError(string.Empty, "Neuspješni pokušaj");
            }
    
            return View(model);
        }
    

    ViewModel

    Under ErrorMessage put your error message

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace MyApplication.ViewModels
    {
        public class LoginViewModel
        {
            [Required(ErrorMessage = "Email je obavezan")]
            [EmailAddress]
            public string Email { get; set; }
    
            [DataType(DataType.Password)]
            [Display(Name = "Lozinka")]
            [Required(ErrorMessage = "Lozinka je obavezna")]
            public string Password { get; set; }
    
            [Display(Name = "Zapamti me")]
            public bool RememberMe { get; set; }
        }
    }
    

    Login View

    @model LoginViewModel
    
    @{
        ViewBag.Title = "Prijava";
    }
    
    

    Prijava

提交回复
热议问题