I have a model and an actionMethod in MVC;
public class employee
{
[Key]
public int id { get; set; }
[Required]
public string employeeID { get; set; }
[Required]
[Remote("doesCnicExist", "employee", AdditionalFields = "employeeID", HttpMethod = "POST", ErrorMessage = "A user with this cnic already exists. Please enter a different cnic.")]
public string cnic { get; set; }
}
[HttpPost]
public JsonResult doesCnicExist(string employeeID, string cnic)
{
var empList = hc.employee.ToList();
bool flag = false;
foreach (employee e in empList)
{
if ((employeeID == e.employeeID) && (cnic == e.cnic))
{
flag = true;
}
}
return Json(flag == false);
}
On Create()
action, it works great. But on Edit()
action, program sees cnic
already exist. And I cannot update employee
with the same cnic
. I cannot figure out how I can use additional employeeID
field to achieve uniquness of employee object while editing?