Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 808
RealityDysfunction Avatar asked Dec 20 '13 20:12

RealityDysfunction


2 Answers

ViewBag is a dynamic wrapper for ViewData, so these two statements are the same:

ViewBag.SuccessBody = TempData["successBody"]; ViewData["SuccessBody"] = TempData["successBody"]; 
like image 34
Moeri Avatar answered Sep 21 '22 11:09

Moeri


Have you tried

ViewBag.SuccessBody = TempData["successBody"]; 
like image 98
Slick86 Avatar answered Sep 18 '22 11:09

Slick86