The following action is scaffold-ed by Visual studio. However, it will raise an exception if the to be deleted record has been referred by another table. I want to show a nice error message when the record has been referred. What's the good ways to do it?
...
// POST: /Venue/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteConfirmed(int id)
{
Venue venue = await db.Venues.FindAsync(id);
db.Venues.Remove(venue);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
Use the HandleException action filter. you can specify what type to handle (SqlException).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With