Cannot apply indexing with [] to an expression of type 'System.Dynamic.DynamicObject'

RealityDysfunction picture RealityDysfunction · Dec 20, 2013 · Viewed 29.6k times · Source

When I try to assign a value to the ViewBag I get the following error:

Cannot apply indexing with [] to an expression of type 'System.Dynamic.DynamicObject'

My code is as follows:

public ActionResult Success()
{
   ViewBag["SuccessBody"] = TempData["successBody"];
   return View();
}

PS: Why I do this you may ask? Because I am redirecting to the Success action and I needed something that persists across redirects. Then, I am assigning the value to ViewBag in order to pass the Value to a 'shared' view.

Answer

Slick86 picture Slick86 · Dec 20, 2013

Have you tried

ViewBag.SuccessBody = TempData["successBody"];