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?
int idandstring employeeID? And you need to show thedoesCnicExist()method you have tried.int idis autogenrated by the database for the table andEmployee IDis assigned by the organization. I am sorry I have been totally unable to think about a possible logic for this situation - fordoesCnicExist(). That's why I asked this question.Create()action. So show it and then it can be corrected to work for theEdit()method. (Bu as far as theidandemployeeIDfields go, what is the point of having 2 identifier fields? - you should be using theidfield inAdditionalFieldssince that's the key)doesCnicExist.