Search code examples
asp.net-mvc-4viewbag

Calling a Partial View and passing multiple ViewBag values as parameters


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.


Solution

  • Just modify your view code with this.

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