2014-11-06 64 views
1

您好我有一些字段验证在MVC 4使用服务层实现服务器侧和客户端验证

表雇员来验证字段我已经创建了两个层

  1. 服务层

  2. 员工存储库

员工存储库代码是

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

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

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 
    } 

最后雇员控制器包括下面结构URE

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


    } 
} 

,我的看法是这样的

enter image description here

我的问题是上面的代码工作正常进行服务器端验证

但我怎么在客户端使用上述相同的实现验证代码 请

回答

1

由于您已经在服务端进行了验证,因此您可以将retu在ModelStateDictionary而不是bool中,您可以检查它在客户端是否有效。 但是这对检查整个服务方法已经完成没有帮助,因此您可以创建一个返回说bool和ModelStateDictionary的新类型。

另一种方法是使用故障例外。您可以创建自己的故障异常,当模型状态无效时将引发异常。此模型状态错误可能包含您的ModelStateDictionary。

所以你有三个选择。

  1. 将返回类型更改为ModelStateDictionary。
  2. 创建一个新的返回类型以返回一个结果和一个ModelStateDictionary。
  3. 使用模型状态无效时发生的故障异常。

就我个人而言,我会使用第三种方法,因为您仍然可以使用您的原始返回类型,然后只需要像发生异常一样捕获故障。这是一个exampleMSDN

+0

0123你可以附加一些代码我返回modelstatedictionary – 2014-11-06 10:47:25

+0

我的想法是我不想发布表单到服务器端进行验证我想获得验证错误在客户端没有使用dataannotation和jQuery的 – 2014-11-06 10:48:45