Q:

Display Exception Message with view in MVC3 RAZOR View Engine

I have a model Like :
public class Post
    {
        public int PostId { get; set; }
        public int ParentId { get; set; }
        public string PostTitle { get; set; }
        public IEnumerable<Post> ChildPosts { get; set; }
    }

I have also a add action in my Controller that return my above Model to add.cshtml view. Now come to the point my application running mode I get a exception (Like Database connection Exception). Now I want to display my exception message in add.cshtml page.
Is it possible? If yes. How can i done this work. Please explain with example code.

A:

If you want to show the error message on the same view:
[HttpPost]
public ActionResult Add(Post post)
{
    try
    {
        // do your database stuff ...
        return RedirectToAction("Success");
    }
    catch (SomeExceptionouNeedToHandle ex)
    {
        ModelState.AddModelError("", ex);
        return View(post);
    }
}

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

Another possibility is to have a generic error page which will be shown when some unexpected exception occurs. Normally you should catch only exceptions that you want to handle. It's bad practice to catch general exceptions in a controller action.
Here's an example of exception handling.

