mvc-unable to make ajax call to controller

I am trying to call my “Account” controller action “ChangePassword” by ajax call.But it says 500 error for controller action.I have tried with or without data but for both it shows same 500 error.

ChangePassword model

  public class ChangePasswordVM
{
    [Required]
    [StringLength(100, ErrorMessage = "The Old Password must be at least {2} characters long.", MinimumLength = 6)]        
    [DataType(DataType.Password)]
    [DisplayName("Old Password")]
    public string OldPassword { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The Password must be at least {2} characters long.", MinimumLength = 6)]
    //[RegularExpression(@"^(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^da-zA-Z])(.{8,15})$", ErrorMessage = "Your password must be between 8 - 15 characters long. It must contain atleast one uppercase,one lowercase,one non-alphabet either number or special character. ")]
    [DataType(DataType.Password)]
    [DisplayName("New Password")]
    public string Password { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [DisplayName("Confirm Password")]
    [System.Web.Mvc.Compare("Password", ErrorMessage = "New Password and confirm password do not match")]
    public string ConfirmPassword { get; set; }

    public AuthenticationProvider AuthProvider { get; set; }
}

ajax call

  <script type="text/javascript">

    function ChangePassword() {
        var model = JSON.stringify({ 'OldPassword': $("#oldPassword").val(), 'Password': $("#newPassword").val(), 'ConfirmPassword': $("#confirmPassword").val() });
    $.ajax({
        cache: false,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: "POST",
        data: model,
        url: '@Url.Action("ChangePassword", "Account")',
        success: function (data) {
            console.log(data);
        }
    });
}

</script>

Controller

 [ValidateAntiForgeryToken]
    [AuthenticationRequired]
    [HttpPost]
    public ActionResult ChangePassword(ChangePasswordVM model)
    {
        if (!ModelState.IsValid)
            return Json(model);

        UserBO user;
        string token;
        PublicLayer.AuthenticateUser(SessionItems.EmailAddress, model.OldPassword, out user, out token);
        if (user == null)
        {
            ModelState.AddModelError("OldPassword", Messages.InvalidPassword);
            return Json(model);
            //return RedirectToAction("MyProfile");
        }

        var salt = Guid.NewGuid();
        user.PasswordSalt = salt;
        user.Password = EncryptionHelper.EncryptPassword(salt.ToString(), model.Password);
        ViewBag.IsPasswordChanged = AdminLayer.UpdateUser(user);

        ModelState.Clear();
        return RedirectToAction("MyProfile");
    }

View

<div class="form-group">
        <div class="col-sm-offset-3 col-sm-5">
            <button class="btn btn-sm btn-primary" onclick="ChangePassword()">Update</button>
        </div>
    </div>


Source: ajax

Leave a Reply