2
votes

Seems like a simple issue but I'm obviously missing something.

I have a ViewBag that I created in a partialView. The PartialView is being rendered by going through

@Html.Action()

In fact that's the ActionMethod inside of which ViewBag variables are being set. Once set they're available inside of the partial View, however when we come back to the parent where Html.Action() is being called from ViewBag variables are not longer available.

Any idea how come ? How can I access the ViewBag variables that were assigned inside of the Html.Action() action method ?

P.S. I saw the following question: Can't access ViewBag in a partial view in ASP.NET MVC3

...From that I gathered that ViewBag variables are passed when you simply render a partial view ( @Html.Partial() ) but they're not when you render a partial view though another Action Method.

Help please ?

1

1 Answers

3
votes

According to this SO post Does a child action share the same ViewBag with its "parents" action?, a child action's ViewData/ViewBag is not accessible from its parent action. This sort of makes sense since information stored in ViewData/ViewBag does not survive redirects, and internally the child action is probably redirecting back to the parent action after it finishes executing. You want to store your data in a location that can survive a single redirect, so try using TempData instead and see if it works.