Getting null values while Remote Validating CurrentPassword

Viewed 14

I have a currentPassword property in my changePasswordViewModel and i want to validate it on client side using Remote attribute. this is my model

 public class PatientPasswordChangeViewModel
    {
        public int id { get; set; }

        [Required(ErrorMessage ="field is required")]
        [DataType(DataType.Password)]
        [Remote("IsCorrectPassword", "Patient", ErrorMessage ="Password is incorrect")]
        [Display(Name ="Current Password")]
        public string OldPassword { get; set; }

        [Required(ErrorMessage ="field is required")]
        [StringLength(100, ErrorMessage = "The {0} must be atleast {2} characters long", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "New Password")]
        public string NewPassword { get; set; }

        [Required(ErrorMessage ="field is required")]
        [DataType(DataType.Password)]
        [Compare("NewPassword",ErrorMessage ="Password Does Not Match")]
        [Display(Name = "Confirm Password")]
        public string ConfirmNewPassword { get; set; }
    }

this is RemoteValidation Code in the controller

public JsonResult IsCorrectPassword([Bind(Prefix = "PatientPasswordChangeViewModel.OldPassword")] string OldPassword, [Bind(Prefix = "PatientPasswordChangeViewModel.id")] int id)
        {   
            var getUserName = context.Patients.Where(q => q.p_id == id).Select(q => q.UserName).FirstOrDefault();
            var getPassword = context.SiteUsers.Where(q => q.UserName == getUserName).Select(q => q.PasswordHash).FirstOrDefault();
            bool status;
            if (PasswordHashManager.ValidatePassword(OldPassword, getPassword))
            {
                status = true;
            }
            else
            {
                status = false;
            }
            return Json(status, JsonRequestBehavior.AllowGet);
        }

And this is the View Code

<section class="password_change-body">

    <h2 id="passheading">Change Password</h2>

    @using (Html.BeginForm("ChangePassword", "Patient", FormMethod.Post))
    {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            @Html.HiddenFor(model => model.id)
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.OldPassword, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.OldPassword, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.OldPassword, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.NewPassword, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.NewPassword, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.NewPassword, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.ConfirmNewPassword, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ConfirmNewPassword, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ConfirmNewPassword, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="change" class="btn btn-primary" />
                </div>
            </div>
        </div>
    }
</section>

I've tried

public JsonResult IsCorrectPassword([Bind(Prefix = "PatientPasswordChangeViewModel")] string OldPassword, [Bind(Prefix = "PatientPasswordChangeViewModel")] int id)
public JsonResult IsCorrectPassword(PatientPasswordChangeViewModel model)

although i'm getting the password value but value for property id is always null. how can i get the value of id.

EDIT I got it working by using AdditionalValues in Remote Attribute used it in my method.

[Remote("IsCorrectPassword", "Patient", ErrorMessage ="Password is incorrect",AdditionalFields ="id")]

and in my controller method

public JsonResult IsCorrectPassword(string OldPassword, int id)
0 Answers
Related