public void InsertStudent(ModelMethodContext context) { var item = new ContosoUniversityModelBinding.Models.Student(); context.TryUpdateModel(item); if (context.ModelState.IsValid) { db.Students.Add(item); db.SaveChanges(); } }
public void addStudentForm_InsertItem() { var item = new ContosoUniversityModelBinding.Models.Student(); TryUpdateModel(item); if (ModelState.IsValid) { using (SchoolContext db = new SchoolContext()) { db.Students.Add(item); db.SaveChanges(); } } }
public void UpdateStudent(int studentID, ModelMethodContext context) { ContosoUniversityModelBinding.Models.Student item = null; item = db.Students.Find(studentID); if (item == null) { context.ModelState.AddModelError("", String.Format("Item with id {0} was not found", studentID)); return; } context.TryUpdateModel(item); if (context.ModelState.IsValid) { db.SaveChanges(); } }
public void DeleteStudent(int studentID, ModelMethodContext context) { var item = new ContosoUniversityModelBinding.Models.Student { StudentID = studentID }; db.Entry(item).State = EntityState.Deleted; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { context.ModelState.AddModelError("", String.Format("Item with id {0} no longer exists in the database.", studentID)); } }
// The id parameter name should match the DataKeyNames value set on the control public void studentsGrid_DeleteItem(int studentID) { using (SchoolContext db = new SchoolContext()) { ContosoUniversityModelBinding.Models.Student item = new ContosoUniversityModelBinding.Models.Student() { StudentID = studentID }; db.Entry(item).State = EntityState.Deleted; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { ModelState.AddModelError("", String.Format("Item with id {0} no longer exists in the database.", studentID)); } } }
// The id parameter name should match the DataKeyNames value set on the control public void studentsGrid_UpdateItem(int studentID) { using (SchoolContext db = new SchoolContext()) { ContosoUniversityModelBinding.Models.Student item = null; item = db.Students.Find(studentID); // Load the item here, e.g. item = MyDataLayer.Find(id); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", studentID)); return; } TryUpdateModel(item); if (ModelState.IsValid) { // Save changes here, e.g. MyDataLayer.SaveChanges(); db.SaveChanges(); } } }