Can’t access ViewBag in a partial view in ASP.NET MVC3

That should work without any problems. In my HomeController Index action I add a message to the ViewBag:

ViewBag.Message = "Welcome to ASP.NET MVC!";

On the Index View I add the partial view:

@Html.Partial("ViewName")

And on the partial view I render the message:

@ViewBag.Message

From the comments below: there seems to be a problem when you pass a model to the partial view. Then you can refer to the original ViewBag with

@ViewContext.Controller.ViewBag.Message

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)