1

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

我确信该模型是正确的,因为它可以在其他页面中使用。在这种情况下, RegisterModelSecondEventFrontEndViewModel的成员。

所以当我评论[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>
4

1 回答 1

2

我在这里找到了正确的答案MVC 3 Client-Side Compare Validation

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

在 ~284 行,你会发现:

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

将其更改为:

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

name 属性需要单引号。


I want to add if you use MIN version u have to change it there as well.

于 2012-04-15T11:55:08.387 回答