Hi am using modelStateDictionary.AddModelError to add validation if there is any errors occurred in model while posting form ..this is working for actual class properties

I have a class like this

public class SuperClass
{
    public EmployeeFamilyTable tbl { get; set; }
}

EmployeeFamilyTable contains some fields those are

public class EmployeeFamilyTable
    {
        public EmployeeFamilyTable()
        {
            familyMembersList = new List<EmployeeFamilyTable>();
        }
        public List<EmployeeFamilyTable> familyMembersList { get; set; }

        public int EmployeeId { get; set; }

        public int EmployeeFamilyDetailId { get; set; }


        public string EmployeeFamilyPersonName { get; set; }


        public DateTime? EmployeeFamilyPersonDateOfBirth { get; set; }
}
}





public interface IEmployeeService
    {
        bool CreateEmployee(EmployeeFamilyTable mainTbl);


    }



public class EmployeeService : IEmployeeService
    {
        private IValidationDictionary _validatonDictionary;
        public EmployeeService(IValidationDictionary validationDictionary)
        {
            _validatonDictionary = validationDictionary;
        }


        public bool ValidateEmployee(EmployeeFamilyTable mainTableToValidate)
        {
            for (int i = 0; i < mainTableToValidate.familyMembersList.Count; i++)
            {
                if (mainTableToValidate.familyMembersList[i].EmployeeFamilyPersonName == null)
                    _validatonDictionary.AddError("mainTableToValidate.familyMembersList[i].EmployeeFamilyPersonName", "Name is required.");
                if (mainTableToValidate.familyMembersList[i].EmployeeFamilyPersonDateOfBirth == null)
                    _validatonDictionary.AddError("EmployeeFamilyPersonDateOfBirth", "Date of birth is required.");
            }
            return _validatonDictionary.IsValid;
        }





        public bool CreateEmployee(EmployeeFamilyTable EmployeeToCreate)
        {
            return (!ValidateEmployee(EmployeeToCreate)) ? false :true;            
        }


    }


}

this is not showing errors in view I think am getting problem in dictionary key

am calling this validation method in controller like this

public ActionResult CreateStudent(SuperClass objstudentmodel)
        {


            if (!_service.CreateEmployee(objstudentmodel.tbl))
            {
//code goes here



public interface IValidationDictionary
    {
        void AddError(string key, string errorMessage);
        bool IsValid { get; }
    }

modelstatewrapperclass contains

public class ModelStateWrapper : IValidationDictionary {

    private ModelStateDictionary _modelState;

    public ModelStateWrapper(ModelStateDictionary modelState)
    {
        _modelState = modelState;
    }

    #region IValidationDictionary Members

    public void AddError(string key, string errorMessage)
    {
        _modelState.AddModelError(key, errorMessage);
    }

    public bool IsValid
    {
        get { return _modelState.IsValid; }
    }

Related posts

Recent Viewed