C# 验证使用MVC4中的服务层实现服务器端和客户端验证
嗨,我有一些字段的表雇员 为了验证字段,我创建了两个层C# 验证使用MVC4中的服务层实现服务器端和客户端验证,c#,asp.net-mvc,validation,C#,Asp.net Mvc,Validation,嗨,我有一些字段的表雇员 为了验证字段,我创建了两个层 服务层 员工存储库 员工存储库代码为 namespace MvcApplication2.Models { public interface IEmployeeMainTableRepository { bool CreateEmployee(EMP_MAIN_TBL EmployeeToCreate); IEnumerable<EMP_MAIN_TBL> ListEmploye
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;
}
}
}
及
最后,employee Controller包含以下结构
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");
}
}
}
我的观点是这样的
我的问题是上面的代码对于服务器端验证来说工作得很好
但如何使用上述代码在客户端实现验证呢
请由于您已经在服务端进行验证,您可以返回ModelStateDictionary而不是bool,然后可以检查它在客户端是否有效。 但是,在检查整个服务方法是否已完成时,这并没有帮助,因此您可以创建一个新类型,返回bool和ModelStateDictionary 另一种方法是使用故障异常。您可以创建自己的错误异常,当模型状态无效时将引发该异常。此模型状态错误可能包含您的ModelStateDictionary 因此,你有三个选择
就我个人而言,我会使用第三种方法,因为您仍然可以使用原始的返回类型,然后只需要捕获错误,就像捕获异常一样。这是一个和您可以附加一些代码吗我在哪里返回ModelStateDictionary我的想法是我不想将表单发布到服务器端进行验证我想在不使用dataannotation和jquery的情况下在客户端获得验证错误
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
}
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");
}
}
}