athira k

athira k

  • NA
  • 140
  • 53.5k

give custom validation in controller action method in aspmvc

Apr 22 2017 2:39 PM
I am doing validation in controller part action method and I got stuck at a point that to give validation for name by checking the condition that the name should not appear already in the table. I have tried many conditions but always getting some error. can anyone please help me to find solution for my problem ???
 
controller
 
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,Name")] Student student)
{
TempData["name"] = student.Name;
 
var show = db.Students.Select(e => e.Name).ToList();
if (show == TempData["name"])
{
ModelState.AddModelError("Name", "Name already exists");
}
 
f (ModelState.IsValid)
{
db.Students.Add(student);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(student);
}
 
this is my controller part where I nned to give validation  

Answers (1)