private void DeleteProduct( Product product ) { //Confirm if (MessageBox.Show (this, $"Are you sure you want to delete '{product.Name}'?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return; else { try { //Delete product _database.Remove (product.Id); UpdateList (); } catch (ArgumentException ex) { MessageBox.Show (ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (ValidationException ex) { MessageBox.Show (ex.Message, "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception) { MessageBox.Show ("Save failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
public IActionResult Delete(ProductModel model) { try { var product = _database.GetAll().FirstOrDefault(p => p.Id == model.Id); _database.Remove(model.Id); return(RedirectToAction(nameof(Index))); } catch (Exception e) { ModelState.AddModelError("", e.Message); }; return(View(model)); }
public ActionResult Delete(int id) { try { var product = _database.GetAll().FirstOrDefault(p => p.Id == id); if (product == null) { return(HttpNotFound()); } _database.Remove(id); return(Content("")); } catch (Exception e) { ModelState.AddModelError("", e.Message); } return(Json(ModelState, JsonRequestBehavior.AllowGet)); }