0

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?

Sparky
  • 98,165
  • 25
  • 199
  • 285
Saeid
  • 13,224
  • 32
  • 107
  • 173
  • 1
    Check this question http://stackoverflow.com/questions/8376322/mvc3-compareattribute-client-side-bug maybe have the same issue. – nemesv Jul 29 '12 at 07:21

1 Answers1

1

You have not refered the script, below I have modifed the coding

Model :

    //[Required(ErrorMessage = "CustomRegex.RequiredErMsg")]
       [Required(ErrorMessage = "Current password is Required")]
    [DataType(DataType.Password)]
    [Display(Name = "Current password")]
    public string OldPassword { get; set; }


      [Required(ErrorMessage = "New password is Required")]
    [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; }
    }

View :

@model test.Models.password

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

@using (Html.BeginForm()) 

{

            <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>
Sham
  • 1,191
  • 2
  • 13
  • 25