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?
Asked
Active
Viewed 3,275 times
3
-
Please add code snippet what you tried. or error log if you are getting any error. – InnocentKiller Feb 07 '14 at 05:46
4 Answers
10
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()

Manoj
- 604
- 4
- 11
0
add model state error in HomeController.cs file catch section like
try
{
methodThrowsError();
}
catch(SomeException ex)
{
ModelState.AddModelError("", ex);
}
return View(yourmodel);
and in cshtml page display error like
@Html.ValidationSummary()
Here's an example of exception handling.
0
Just create a session variable in your controller AS :
public ActionResult DeleteConfirmed(int id)
{
try
{
//////whatever code u want
return RedirectToAction("Index");
}
catch (Exception ex)
{
string m="mysession";
Session["dep"] = m;}
return view();}
and use it as a flag variable.. then in your razor view Show your message as :
if (Session["dep"] != null)
{
<h1>Whatever message you want to show in your view</h1>
}
Rate my answer please if it was helpfull

Hamza
- 1
- 1
0
[HttpPost]
public ActionResult MethodName(Model model)
{
try
{
// your business logic if need
return RedirectToAction("Success");
}
catch (Exception ex)
{
ModelState.AddModelError("", ex.Message);
return View(model);
}
}

dilipkumar1007
- 329
- 4
- 22