验证在MVC 4使用服务层来实现服务器端和客户端验证

您好我有一些字段的表员工

Hi I have table employee with some fields

要验证我已经创建了两层领域

to validate fields I have created two layers


  1. 服务层

  1. Service layer

员工信息库

员工信息库code是

namespace MvcApplication2.Models
{

    public interface IEmployeeMainTableRepository
    {
        bool CreateEmployee(EMP_MAIN_TBL EmployeeToCreate);
        IEnumerable<EMP_MAIN_TBL> ListEmployees();
    }


    public class EmployeeRepository : MvcApplication2.Models.IEmployeeMainTableRepository
    {
        private EMPLOYEE_SYSTEMEntities _entities = new EMPLOYEE_SYSTEMEntities();


        public IEnumerable<EMP_MAIN_TBL> ListEmployees()
        {
            return _entities.EMP_MAIN_TBL.ToList();
        }


        public bool CreateEmployee(EMP_MAIN_TBL EmployeeToCreate)
        {
            try
            {
              // _entities.AddToEMP_MAIN_TBL(productToCreate);
                _entities.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }


    }

和服务层包含

    public interface IEmployeeService

    {
        bool CreateEmployee(EMP_MAIN_TBL EmployeeToCreate);
        System.Collections.Generic.IEnumerable<EMP_MAIN_TBL> ListEmployees();
    }

    public class EmployeeService : MvcApplication2.Models.IEmployeeService
    {


        private IValidationDictionary _validatonDictionary;
        private IEmployeeMainTableRepository _repository;

        public EmployeeService(IValidationDictionary validationDictionary, IEmployeeMainTableRepository repository)
        {
            _validatonDictionary = validationDictionary;
            _repository = repository;
        }



        protected bool ValidateEmployee(EMP_MAIN_TBL employeeToValidate)
        {
            if (employeeToValidate.EMP_NM == null)
                _validatonDictionary.AddError("EMP_NM", "Name is required.");
            if (employeeToValidate.PLCE_OF_BRTH == null)
                _validatonDictionary.AddError("PLCE_OF_BRTH", "Place of birth is required.");

            return _validatonDictionary.IsValid;
        }

        public IEnumerable<EMP_MAIN_TBL> ListEmployees()
        {
            return _repository.ListEmployees();
        }

        public bool CreateEmployee(EMP_MAIN_TBL EmployeeToCreate)
        {
            // Validation logic
            if (!ValidateEmployee(EmployeeToCreate))
                return false;

            // Database logic
            try
            {
                _repository.CreateEmployee(EmployeeToCreate);
            }
            catch
            {
                return false;
            }
            return true;
        }

和我已经创建了两个类来添加验证消息

and I have created two more classes to add validation messages

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

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; }
        }

        #endregion
    }

终于员工控制器包含以下结构

finally employee controllers contains below structure

  public class EmployeeController : Controller
    {
        private IEmployeeService _service;

        public EmployeeController()
        {
            _service = new EmployeeService(new ModelStateWrapper(this.ModelState), new EmployeeRepository());
        }

        public EmployeeController(IEmployeeService service)
        {
            _service = service;
        }


        public ActionResult Index()
        {
            return View(_service.ListEmployees());
        }


        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            return View(new EMP_MAIN_TBL());
        }

        //
        // POST: /Product/Create

        [AcceptVerbs(HttpVerbs.Post)]
        [HttpPost]
        public ActionResult Create([Bind(Exclude = "EMP_ID")] EMP_MAIN_TBL employeeToCreate)
        {
            if (!_service.CreateEmployee(employeeToCreate))
                return View();
            return RedirectToAction("Index");
        }


    }
}

和我的看法是这样的。

我的问题是上面code工作罚款服务器端验证

My question is above code working fine for server side validation

但我怎么使用上面相同的code ++实现对客户端验证

but how do I achieve validation on client side using above same code please



 1 条回答