我使用以下模型来控制用户输入并注册他。但是,如果我输入相同的密码并按下按钮,则会出现有关确认密码错误的消息。
我确信该模型是正确的,因为它可以在其他页面中使用。在这种情况下, 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>