i have a problem with auto incremental primary keys..
when im adding a student to my database, i can add the same student over and over again because the primary key of student is auto incremental so it is, of course, never repeated, i need to verify that the SSN of the student does not already exist in the database before inserting.
how can i do that on mvc3?? (im totally new in .net and mvc3)
my code for create in the student controller is:
[HttpPost]
public ActionResult Create(Alumno alumno)
{
try
{
using (var db = new administracionEntities())
{
db.Alumno.Add(alumno);
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
before you insert check if input alumno.ssn doesnot exist like
if this returns true, then there exists a ssn in database.
There are different ways in which you can achieve this in MVC.In one of which, You can have this call in a separate call to controller than in Create Action. May be you can check if duplicate SSN exists through JQuery.Ajax call on SSN textbox blur event on client side.
This is controller action