-1

I have a PartialView called TopPanel. This Panel is responsible for displaying error messages generated on any page in my application. To handle this, when any exception occurs on a page, I have it call an "ErrorHandler" action in the TopPanel controller. ErrorHandler updates the ViewBag with the error message and calls the Index action which just returns the partial view(for now, since im testing. I will have it call the Main Controllers Index Action later to display the whole page). My understanding is that calling the Index action will reload the view and the ErrorDiv that I have on TopPanels PartilaView will be able to display the new error message in ViewBag. However, nothing gets displayed and I'm not sure why.

Heres some code -

The ErrorHandler Action -

public ActionResult ErrorHandler(string message)
{
        ViewBag.ErrorMsg = message;
        return RedirectToAction("Index");
}

I've checked in the debugger, "message" does have a valid value. And ViewBag.ErrorMsg does get populated as well.

Index Action of TopPanel -

 public ActionResult Index()
 {           
        return PartialView();
 }

TopPanels PartialView contains this lone which displays the error -

<div id="errorMsgBox">@ViewBag.ErrorMsg</div>

Can anyone point out what the issue is?

neuDev33
  • 1,573
  • 7
  • 41
  • 54

2 Answers2

2

Since you are doing a redirect, you are actually going to end up in a completely separate Request/Response which has no knowledge of the previous ViewBag property value.

Josh
  • 44,706
  • 7
  • 102
  • 124
2

RedirectToAction is like a browser redirect. It actually sends the redirect code to the browser and the action name is the new URL to request. As such you're actually sending a new request to the server for a new page, which will have its own ViewBag.

Instead of RedirectToAction you might just want to return the View and specify the PartialView name.

Steve Owen
  • 2,022
  • 1
  • 20
  • 30
  • +1 for giving me the alternate solution. However, I have a doubt. If I want to load just the TopPanel, returning the PartialView of TopPanel is just going to display that much. Is there a way to achieve this? Or do I have to reload the entire page(parent controller that has all the partial views?) – neuDev33 Jul 05 '12 at 16:49
  • your understanding of partials, actions and redirects is wrong. You should go throuh a tutorial of some sort because it's not easy to explain everything here. Also this is not the way to handle errors in MVC apps. – mare Jul 05 '12 at 19:46