MVC3中的确认密码问题

时间:2012-04-15 11:41:30

标签: c# .net asp.net-mvc-3

我使用以下模型来控制用户输入并注册他。 但是,如果我输入相同的密码并按下按钮,则会显示有关该确认密码错误的消息。

我确信该模型是正确的,因为它适用于其他页面。 在这种情况下, RegisterModelSecond EventFrontEndViewModel 的成员。

因此,当我评论[Compare("RegisterModel.Password", ErrorMessage = "The password and confirmation password do not match.")]时,它有效,但我无需确认!

有任何线索如何解决?

public class EventFrontEndViewModel
    {
        public Page CurrentPage { set; get; }

        public List<Event> Events { set; get; }

        public List<Event> SubscribedEvents { set; get; }

        public RegisterModelSecond RegisterModel { set; get; }

        public EventFrontEndViewModel()
        {
            CurrentPage = new Page();
            Events = new List<Event>();
            RegisterModel = new RegisterModelSecond();
            SubscribedEvents = new List<Event>();
        }
    }

注册模型

public class RegisterModelSecond
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email address")]
        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; }

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

和HTML

<div class="editor-label">
                                        @Html.LabelFor(m => m.RegisterModel.Password)
                                    </div>
                                    <div class="editor-field">
                                        @Html.PasswordFor(m => m.RegisterModel.Password)
                                        @Html.ValidationMessageFor(m => m.RegisterModel.Password)
                                    </div>
                                    <div class="clear">
                                    </div>
                                    <div class="editor-label">
                                        @Html.LabelFor(m => m.RegisterModel.ConfirmPassword)
                                    </div>
                                    <div class="editor-field">
                                        @Html.PasswordFor(m => m.RegisterModel.ConfirmPassword)
                                        @Html.ValidationMessageFor(m => m.RegisterModel.ConfirmPassword)
                                    </div>
                                    <div class="clear">
                                    </div>

1 个答案:

答案 0 :(得分:2)

我在MVC 3 Client-Side Compare Validation

找到了正确的答案

这是客户端验证脚本中的错误:jquery.validate.unobtrusive.js

在Line~284你会发现:

element = $(options.form).find(":input[name=" + fullOtherName + "]")[0];

将其更改为:

element = $(options.form).find(":input[name='" + fullOtherName + "']")[0];

name属性需要单引号。


我想添加如果您使用MIN版本,您也必须在那里进行更改。