I got table with these columns : Id , Name, Graduated
In Create Action checking for duplicate is working fine :
if(db.students.Any(a=>a.Name.Equals(student.Name)
{
ModelState.AddModelError("Name","Name already exists!!")
}
How can I use it in Edit Action just only if Name has changed then check duplicate?? Because when I use it and just update the Graduation Date and click Save it shows duplicate error. Thank you in advanced.
You can exclude the entry you are editing from the Any()
condition. It can look like this:
if (db.students.Any(a => a.Name.Equals(student.Name) && a.Id != student.Id)) {
// ...
}
This way it will check only all the other students, not the one you are about to edit.