3

I have the following

public ActionResult Index()    
{
 ViewBag.Stage1="Module1";
 ViewBag.Stage2="Module2";
 ....
}

Now within View I am calling a partial View and passing ViewBag

@Html.Partial("MyMenu", new ViewDataDictionary(new { Stage1= ViewBag.Stage1, Stage2= ViewBag.Stage2}))

But here I am getting error 'object' does not contain a definition for 'Stage1' I even tried using something like

@Html.Partial("_LeftMenu", new {Level1=ViewData["Level1"],Level2=ViewData["Level2"] })

The same thing continues here as well the thing is I need to pass two values and render some Html based on values in ViewBag. Thanks any help is highly appreciated. Please correct me if I am wrong somewhere. Thanks.

Zaker
  • 537
  • 16
  • 30
  • 1
    You should be able to access the viewbag data within the partial view if you call `@Html.Partial("MyMenu")`. But this might not work if you also want to pass a model. – elolos May 06 '15 at 09:31
  • @elolos I am ain't passing any model except the ViewBag's as mentioned. – Zaker May 06 '15 at 09:35
  • Have you tried accessing the Viewbag within the partial view without passing it through the @Html helper? – elolos May 06 '15 at 09:48

1 Answers1

4

Just modify your view code with this.

@Html.Partial("MyMenu", new ViewDataDictionary{{"Stage1", ViewBag.Stage1},{"Stage2", ViewBag.Stage2}})
Jinesh Jain
  • 1,232
  • 9
  • 23
  • I get an error saying Cannot perform runtime binding on a null reference. – Zaker May 06 '15 at 09:57
  • can u please share your partial view "MyMenu" code as same code working at my side. In my "MyMenu" partial view i have only this code @{ Response.Write("hello " + ViewData["Stage1"]); } – Jinesh Jain May 06 '15 at 10:02