Jquery 在MVC中插入重复值时显示验证错误
在MVC项目中工作 要在插入重复记录时生成错误消息吗 如何在create方法中检查重复记录Jquery 在MVC中插入重复值时显示验证错误,jquery,asp.net-mvc,asp.net-mvc-3,model-view-controller,Jquery,Asp.net Mvc,Asp.net Mvc 3,Model View Controller,在MVC项目中工作 要在插入重复记录时生成错误消息吗 如何在create方法中检查重复记录 [HttpPost] public ActionResult Create(Employee _Checkin) { if (ModelState.IsValid) { Utilities.SaveEntityInstance(_Checkin); retur
[HttpPost]
public ActionResult Create(Employee _Checkin)
{
if (ModelState.IsValid)
{
Utilities.SaveEntityInstance(_Checkin);
return new AjaxAwareRedirectResult("/Checkin/Index");
}
else
{
return PartialView("Create", _Checkin);
}
}
插入新员工表单时,字段为
empname empjoindate empid
如果我再次尝试插入表中显示的相同员工信息
我应该给你留个口信
我该怎么做 您可以检查用户名是否已经存在(理想情况下是检查数据库代码,以保存Emp ID是否已经存在。然后使用以下方法: 编辑-此处的第一个参数应与模型中要添加错误的字段匹配
ModelState.AddModelError("EmpName", "Employee Name already exists. Please enter a different Employee Name.");
在Utilities.SaveEntityInstance(_Checkin);)之前,只需检查用户是否存在。或者修改Utilities.SaveEntityInstance(_Checkin)代码以在用户存在时引发异常(理想情况下检查empid)。