private void Delete_Click(object sender, EventArgs args) { int id = (int)dataGridView1.SelectedCells[0].Value; var contactToDelete = (from c in _entities.Contacts where c.Id == id select c).FirstOrDefault(); if (MessageBox.Show(string.Format("Do you want to delete {0} {1} ?", contactToDelete.FirstName, contactToDelete.LastName), "Delete contact", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { _entities.DeleteObject(contactToDelete); _entities.SaveChanges(); IndexView(); } }
public ActionResult Create([Bind(Exclude = "Id")] Contact contactToCreate) { // Validation logic ValidateContact(contactToCreate); if (!ModelState.IsValid) { return(View()); } // Database logic try { _entities.AddToContactSet(contactToCreate); _entities.SaveChanges(); return(RedirectToAction("Index")); } catch { return(View()); } }