Assume this model:
public class ChangePasswordModel {
[Required(ErrorMessage = CustomRegex.RequiredErMsg)]
[DataType(DataType.Password)]
[Display(Name = "Current password")]
public string OldPassword { get; set; }
[Required(ErrorMessage = CustomRegex.RequiredErMsg)]
[RegularExpression(CustomRegex.PasswordRX, ErrorMessage = CustomRegex.PasswordErMsg)]
[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; }
}
OK, every thing worked perfectly with this view:
@model ChangePasswordModel
@{
ViewBag.Title = "Profile";
}
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Change Password</legend>
<dl>
<dt>@Html.LabelFor(model => model.OldPassword)</dt>
<dd>
@Html.EditorFor(model => model.OldPassword)
@Html.ValidationMessageFor(model => model.OldPassword, null, new { @class = "invalid-side-note" })
</dd>
<dt>@Html.LabelFor(model => model.NewPassword)</dt>
<dd>
@Html.EditorFor(model => model.NewPassword)
@Html.ValidationMessageFor(model => model.NewPassword, null, new { @class = "invalid-side-note" })
</dd>
<dt>@Html.LabelFor(model => model.ConfirmPassword)</dt>
<dd>
@Html.EditorFor(model => model.ConfirmPassword)
@Html.ValidationMessageFor(model => model.ConfirmPassword, null, new { @class = "invalid-side-note" })
</dd>
</dl>
<input type="submit" value="Save" class="button red inframebutton" />
</fieldset>
}
So I need use some generic model :
public class ViewModel<T> {
public T MainModel { get; set; }
public ViewPart ViewPart { get; set; }
}
Then I passed to view this generic view and changed view as:
@model ViewModel<ChangePasswordModel>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Change Password</legend>
<dl>
<dt>@Html.LabelFor(model => model.MainModel.OldPassword)</dt>
<dd>
@Html.EditorFor(model => model.MainModel.OldPassword)
@Html.ValidationMessageFor(model => model.MainModel.OldPassword, null, new { @class = "invalid-side-note" })
</dd>
<dt>@Html.LabelFor(model => model.MainModel.NewPassword)</dt>
<dd>
@Html.EditorFor(model => model.MainModel.NewPassword)
@Html.ValidationMessageFor(model => model.MainModel.NewPassword, null, new { @class = "invalid-side-note" })
</dd>
<dt>@Html.LabelFor(model => model.MainModel.ConfirmPassword)</dt>
<dd>
@Html.EditorFor(model => model.MainModel.ConfirmPassword)
@Html.ValidationMessageFor(model => model.MainModel.ConfirmPassword, null, new { @class = "invalid-side-note" })
</dd>
</dl>
<input type="submit" value="Save" class="button red inframebutton" />
</fieldset>
}
Then I have a problem with compare
validation as you see in model compare validation must compare ConfirmPassword
with NewPassword
but with this new changes the ConfirmPassword
compared with OldPassword
that's so wired. I think this is maybe because of change in names or Ids of elements so I try
[Compare("MainModel.NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
And
[Compare("MainModel_NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
but none of them not worked, what's the problem? and how can I use compare validation with this new model? is there any way?