I was stuck with some validations for quite a while. I have been thoroughly searching the internet to find ways to apply the validations I require. I came across many methods for validations even certain Nuget packages. But in most cases my requirement was not fulfilled. I did a remote validation, which for me was the easiest to comprehend. But it did not give me the expected results in some cases.
I got suggestions from many asking me to do Client side validation using Jquery and leaving the Server side validation for the time being (Most of the advices were not pretty clear for me, I have never been to web development before). These languages being really difficult for me to understand, I had to keep on looking for ways to get my things done. Maybe I was in search of the easiest way to do a validation.
Finally I was successful in getting the desired output by validating my data in the controller Action method. I am not sure if it is a conventional method to follow or if I would end up with serious consequences at a later stage.
So now I am really wondering why nobody adviced me to do the validation in the action method when it is possible!! Is my approach right to do a valiadtion. Just for reference I am adding the validation that I have done.
if (ModelState.IsValid)
{
if (db.SystemFamily.Any(x => x.FamilyName.Equals(systemFamily.FamilyName)))
{
ModelState.AddModelError("FamilyName", "Already exists");
return View(systemFamily);
}
else
{
systemFamily.DateCreated = DateTime.Now;
systemFamily.CreatedBy = User.Identity.Name;
db.SystemFamily.Add(systemFamily);
db.SaveChanges();
return RedirectToAction("Index");
}
}
I am looking for flaws in my approach and the possible easiest alternative for my task.