我已经完全按照 Darin Dimitrov 在以下位置提供的 NotEqual 属性实现了: https ://stackoverflow.com/a/5742494/1946707
我在这里遇到的问题是我正在使用一个 jQuery 对话框,并且希望在我的对话框的客户端获得验证。我需要的验证触发,但不相等的验证在客户端不起作用......任何帮助将不胜感激。
这是我的模型:
public class ChangePasswordModel
{
[Required]
[DataType(DataType.Password)]
[Display(Name = "Current password")]
public string OldPassword { get; set; }
[Required]
[NotEqual("OldPassword", ErrorMessage = "should be different than Prop1")]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
这是我的看法:
@using (Html.BeginForm("ChangePassword", "Account", new { area = "" }, FormMethod.Post, new { id = "changePasswordForm" }))
{
<a href="#" onclick="openChangePasswordDialog();">Change Password</a>
<div id="dialogChangePassword" title="Change Password">
<table>
<tr>
<td>@Html.LabelFor(m => m.OldPassword)
</td>
<td>@Html.PasswordFor(m => m.OldPassword)
</td>
<td>@Html.ValidationMessageFor(m => m.OldPassword)
</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.NewPassword)
</td>
<td>@Html.PasswordFor(m => m.NewPassword)
</td>
<td>@Html.ValidationMessageFor(m => m.NewPassword)
</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.ConfirmPassword)
</td>
<td>@Html.PasswordFor(m => m.ConfirmPassword)
</td>
<td>@Html.ValidationMessageFor(m => m.ConfirmPassword)
</td>
</tr>
</table>
</div>
}
这是我的javascript:
var changePasswordDialogId = '#dialogChangePassword';
var oldPasswordInput = '#changePasswordForm #OldPassword';
var newPasswordInput = '#changePasswordForm #NewPassword';
var confirmPasswordInput = '#changePasswordForm #ConfirmPassword';
$(document).ready(function () {
$(changePasswordDialogId).dialog(
{
autoOpen: false,
autoResize: true,
modal: true,
open: function () {
$(this).parent().appendTo("#changePasswordForm");
},
buttons: {
Ok: function () {
if ($(oldPasswordInput).valid() && $(newPasswordInput).valid() && $(confirmPasswordInput).valid()) {
alert('ready to post!');
$(changePasswordDialogId).dialog('close');
}
},
Cancel: function () {
$(changePasswordDialogId).dialog('close');
}
}
});
}); //document.ready
function openChangePasswordDialog() {
$(changePasswordDialogId).dialog('open');
}