locked
vs2013+mvc5+asp.net Identity-This 密码 and 确认密码 do not match RRS feed

  • Question

  • I would finish a Register.cshtml, there is a password and confirmpassword. I need Chinese to finish confirmpassword, so I do like this in  AccountViewModels.cs:

    [DataType(DataType.Password)]
    [Display(Name = "确认密码")]
    [Compare("Password", ErrorMessage = "输入的密码不一致.")]
    public string ConfirmPassword { get; set; }
    //确认密码 in English is ConfirmPassword.
    //"输入的密码不一致." in english is "Password and ConfirmPassword do not match."

    So,When running the Register.cshtml, if Password and ConfirmPassword do not match, the ErrorMessage should be: 输入的密码不一致. But , in fact, the ErrorMessage display with Chinese and English mixed, It's: This 密码 and 确认密码 do not match.

    I don't know If I have myself clear. could anybody help me ?
    • Edited by achinaboy Saturday, April 26, 2014 2:03 AM made it clear
    • Moved by Amanda Zhu Thursday, May 1, 2014 1:27 AM
    Saturday, April 26, 2014 1:59 AM

Answers

  • Hello,

    Thank you for your post.

    I am afraid that the issue is out of support range of VS General Question forum which mainly discusses the usage of Visual Studio IDE such as WPF & SL designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System and Visual Studio Editor.

    Because you are working with ASP.NET MVC Application, I suggest that you can consult your issue on ASP.NET forum: http://forums.asp.net/  for better solution and support.

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Amanda Zhu Tuesday, April 29, 2014 3:30 AM
    • Marked as answer by Just Karl Thursday, May 15, 2014 8:50 PM
    Tuesday, April 29, 2014 3:30 AM

All replies

  • Hello,

    Thank you for your post.

    I am afraid that the issue is out of support range of VS General Question forum which mainly discusses the usage of Visual Studio IDE such as WPF & SL designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System and Visual Studio Editor.

    Because you are working with ASP.NET MVC Application, I suggest that you can consult your issue on ASP.NET forum: http://forums.asp.net/  for better solution and support.

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Amanda Zhu Tuesday, April 29, 2014 3:30 AM
    • Marked as answer by Just Karl Thursday, May 15, 2014 8:50 PM
    Tuesday, April 29, 2014 3:30 AM
  • A decent way is to use resource files for Chinese language. Create a resource file for Chinese language that contains key value pairs. Then in the attributes specify the ErrorMessageResourceType  like

     public class Role : Core
        {
            [Display(ResourceType=typeof(Project1.Resource.Resource), Name="Name")]
            [Compare(ErrorMessageResourceType=typeof(Project1.Resource.Resource), ErrorMessageResourceName = "ComparePasswordMismatchMessage")]
            public string ConfirmPassword { set; get; }
    
        }

    You also have to set the culture of current thread which you can write it in the ExecuteCore method of base controller class. In order to do this create your custom controller class that should be derived from Controller and then override ExceuteCode method and specify this code.

       protected override void ExecuteCore(){
            
                
                string cultureName = "en-US";
    
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);
                Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureName);
               
                base.ExecuteCore();
            }

    Moreover your resource files should follow the naming conventions. For example if its US English it should be resource.en-US.resx and so on.

    Hope this helps!


    Ovais Mehboob Ahmed Khan http://ovaismehboob.wordpress.com


    Wednesday, April 30, 2014 9:02 AM
  • I did,but it doesn't wok. May be I didn't make myself clear.

    thanks.

    Controller

     public class AccountController : Controller
        {
            protected override void ExecuteCore()
            {
                string cultureName = "zh-CN";
                System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(cultureName);
                System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.CreateSpecificCulture(cultureName);
    
                base.ExecuteCore();
            }
    
    
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
            {
                if (ModelState.IsValid)
                {
                    var user = new ApplicationUser() { UserName = model.UserName ,PhoneNum=model.PhoneNum
                        ,Email=model.Email,RealName=model.RealName,CreatDate=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                    ,UserTypeCode=Common.parameter_UserType.学员.ToString() ,Gender=model.Gender};
                    var result = await UserManager.CreateAsync(user, model.Password);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
    
                // 如果我们进行到这一步时某个地方出错,则重新显示表单
                return View(model);
            }
    
    }
    Models:
    public class RegisterViewModel
        {
            [Required(ErrorMessage = "请填写登录账号(2~18个字符).")]
            [RegularExpression(@"^[a-z|A-Z|0-9|\u4e00-\u9fa5]{1}[a-z|A-Z|0-9|_|\u4e00-\u9fa5]{0,16}[a-z|A-Z|0-9|\u4e00-\u9fa5]{1}$"
                , ErrorMessage = "请用数字或字母填写登录账号(2~18个字符).")]
            [Display(Name = "登录账号")]
            public string UserName { get; set; }
    
            [Required(ErrorMessage = "请填写登录密码.")]
            [StringLength(18, ErrorMessage = "{0} {2}~{1} 个字符.", MinimumLength = 6)]
            [DataType(DataType.Password)]
            [Display(Name = "登录密码")]
            public string Password { get; set; }
    
            [DataType(DataType.Password)]
            [Display(Name = "确认密码")]
            //[Compare("Password", ErrorMessage = Resources.Global_CN.ConfirmPasswordErrorMessage+ "输入的登录密码不一致.")]
            [Compare("Password",ErrorMessageResourceType = typeof(Resources.Global_CN), ErrorMessageResourceName = "ConfirmPasswordErrorMessage")]
            public string ConfirmPassword { get; set; }
    }

    Register.cshtml

    @model LectureTheater.Models.RegisterViewModel
    @{    ViewBag.Title = "学员注册";}
    
    @*<h2>@ViewBag.Title。</h2>*@
    
    @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()
        <h4>学员注册</h4>
        <hr />
        @Html.ValidationSummary()
        <div class="form-group">
            @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
                @Html.ValidationMessageFor(m => m.UserName)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
                @Html.ValidationMessageFor(m => m.Password)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
                @Html.ValidationMessageFor(m => m.ConfirmPassword)
            </div>
        </div>
    
    
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" class="btn btn-default" value="注 册" />
            </div>
        </div>
    }
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }


    Removed PII
    • Edited by ZigZag3143x Saturday, August 15, 2015 10:33 AM Removed PII
    Thursday, May 1, 2014 2:53 AM