Monday, 18 March 2013

Data Annotations Remote Validation with AdditionalFields in Action Method : MVC 4



Model:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Web.Mvc;

public class ChangePasswordViewModel
    {
        private int _UserNid = -1;
        [ReadOnly(true)]
        public int UserNid
        {
            get { return this._UserNid; }
            set { this._UserNid = value; }
        }

        private string _OldPassword = string.Empty;       
        [DisplayName("Old Password :")]
        [DataType(DataType.Password)]
        [StringLength(20, ErrorMessage="Must be between 8 and 20 characters", MinimumLength=8)]
        [Required(ErrorMessage="Please enter Old Password")]
        [Remote("ValidateOldPassword", "Account", ErrorMessage="Old password doesn't match")]
        public string OldPassword
        {
            get { return this._OldPassword; }
            set { this._OldPassword = value; }
        }

        private string _NewPassword = string.Empty;
        [DisplayName("New Password :")]
        [DataType(DataType.Password)]
        [StringLength(20, ErrorMessage="Must be between 8 and 20 characters", MinimumLength=8)]
        [Required(ErrorMessage="Please enter New Password")]
        public string NewPassword
        {
            get { return this._NewPassword; }
            set { this._NewPassword = value; }
        }

        private string _ConfirmPassword = string.Empty;
        [DisplayName("Confirm Password :")]
        [DataType(DataType.Password)]
        [StringLength(20, ErrorMessage="Must be between 8 and 20 characters", MinimumLength=8)]
        [Required(ErrorMessage="Please enter Confirm Password")]
        [CompareAttribute("NewPassword", ErrorMessage="The New Password and Confirm New Password fields did not match")]
        public string ConfirmPassword
        {
            get { return this._ConfirmPassword; }
            set { this._ConfirmPassword = value; }
        }
    }


Controller:

public class AccountController : Controller
    {
public JsonResult ValidateOldPassword(string oldPassword)
        {
            bool RetVal = false;
            int UserNid = -1;           
           
            UserNid = Convert.ToInt32(HttpContext.User.Identity.Name);            

            //-- Validate user from db
            RetVal = this._UserRepository.IsPasswordValidated(UserNid, oldPassword);           

            return Json(RetVal, JsonRequestBehavior.AllowGet);
        }
    }



View:

@using GMISRwanda.ViewModel.Account

@model ChangePasswordViewModel

@{
    ViewBag.Title = "Change Password";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}

@using (Html.BeginForm("ChangePassword", "Account", FormMethod.Post))
{
    <h2>ChangePassword</h2>
   
    @Html.HiddenFor(m => m.UserNid)

    <div>
        @Html.LabelFor(m => m.OldPassword)       
        @Html.PasswordFor(m => m.OldPassword)
        @Html.ValidationMessageFor(m => m.OldPassword)
    </div>
   
    <div>
        @Html.LabelFor(m => m.NewPassword)
        @Html.PasswordFor(m => m.NewPassword)
        @Html.ValidationMessageFor(m => m.NewPassword)
    </div>
   
    <div>
        @Html.LabelFor(m => m.ConfirmPassword)
        @Html.PasswordFor(m => m.ConfirmPassword)
        @Html.ValidationMessageFor(m => m.ConfirmPassword)
    </div>
   
    <div>
        <input type="submit" name="Ok" value="OK" />
        <input type="reset" name="Reset" value="Reset" />
        <input type="button" name="Cancel" value="Cancel" onclick="location.href='@Url.Action("UserDetails", "Admin")'" />
    </div>
}




No comments:

Post a Comment