It has helped a lot. In my case, it was a table where updates were also possible. In this case, the indicated solution does not work. So I wanted to share my decision for this case.
In the solution below, I added an additional field to go to the controller (the main key of the model). Then in the controller I check to see if a primary key is set. If so, we know that we came from the update site, as this is the only case when we already have an identifier in the model. The final step is to check if the row and primary key are the same. If both of them, this is normal, because we did not change anything in the line. If only the string is the same but not the identifier, this means that we changed the string and changed it to another existing string, so we return false.
Model:
[Key] [Display(Name = "Idee ID")] public int intIdeaID { get; set; } [Required(ErrorMessage = "Dieses Feld muss ausgefΓΌllt werden")] [Display(Name = "Idee")] [Remote("ideaExists", "TabIdea", HttpMethod = "POST", ErrorMessage = "Es wurde bereits eine Idee mit dieser Bezeichnung erstellt", AdditionalFields = "intIdeaID")] public string strIdea { get; set; }
Controller:
[HttpPost] public JsonResult ideaExists(string strIdea, int? intIdeaID) { if (intIdeaID != null) { if (db.tabIdea.Any(x => x.strIdea == strIdea)) { tabIdea existingTabIdea = db.tabIdea.Single(x => x.strIdea == strIdea); if (existingTabIdea.intIdeaID != intIdeaID) { return Json(false); } else { return Json(true); } } else { return Json(true); } } else { return Json(!db.tabIdea.Any(x => x.strIdea == strIdea)); } }
source share