Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing exception message from controller class to view page

I have defined a method in try catch block in HomeController.cs in my MVC3 Razor project. Now please tell how wil I show this exception message on my view page?

like image 631
dilipkumar1007 Avatar asked Feb 18 '26 10:02

dilipkumar1007


1 Answers

If you want to show the error message on the same view:

[HttpPost]
public ActionResult Add(Model model)
{
    try
    {
        // some code...
        return RedirectToAction("Success");
    }
    catch (SomeException ex)
    {
        ModelState.AddModelError("", ex.Message);
        return View(model);
    }
}

and inside your Add.cshtml view you could use the ValidationSummary helper to display the error message:

@Html.ValidationSummary()
like image 178
Manoj Avatar answered Feb 21 '26 14:02

Manoj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!