Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying MVC 3 ViewBag in a partial view does not persist to the _Layout.cshtml

I am using MVC 3 with the Razor view engine. I want to set some values in the ViewBag inside a Partial View and want retrieve those values in my _Layout.cshtml. For example, when you setup a default ASP.NET MVC 3 project you get a _Layout.cshtml file in the "/Views/Shared" folder. In that _Layout.cshtml the Page Title is set like this:

<title>@ViewBag.PageTitle</title> 

Then in "/Views/Home/About.cshtml" view the contents of the ViewBag are modified:

@{     ViewBag.Title = "About Us"; } 

This works fine. When the About view is rendered the page title is "About Us". So, now I want to render a Partial view inside my About view and I want to modify the ViewBag.Title inside my Partial view. ("/Views/Shared/SomePartial.cshtml")

@Html.Partial("SomePartial") 

In this Partial view I have this code:

@{     ViewBag.Title = "About Us From The Partial View"; } 

When I debug this code I see the ViewBag.Title get set to "About Us" and then in the Partial view I see it get reset to "About Us From The Partial View", but when the code hits the _Layout.cshtml it goes back to "About Us".

Does this mean that if the contents of the ViewBag are modified in a Partial view, those changes will not appear(be accessible) in the main view (About.cshtml) or the _Layout.cshtml?

Thanks in advance!

like image 318
Zoran Avatar asked Feb 11 '11 21:02

Zoran


People also ask

How do I change the ViewBag value in Cshtml?

The ViewBag object value will be set inside Controller and then the value of the ViewBag object will be accessed in the cshtml file (View) using Razor syntax in ASP.Net MVC Razor.

Can we use ViewBag in partial view?

2. Pass Data to Partial View using ViewBag/ViewData. You can use ViewData / ViewBag to pass the information from the controller's action method to the View. ViewData uses ViewDataDictionary and ViewBag is just a wrapper around ViewData using dynamic feature.

Which is faster ViewData or ViewBag?

ViewBag will be slower than ViewData; but probably not enough to warrant concern.

Is ViewBag slower than ViewData in MVC?

Yes, ViewBag is slower than ViewData in MVC.


1 Answers

If you pass the ViewBag into the partial's viewdatadictionary, then pull it out (and cast), you can do whatever you want and the reference is kept. The cool part is that since it's dynamic, you can even add properties and then they'll show up on the parent page's Viewbag.

Page:

//set the viewbag into the partial's view data @{Html.RenderPartial("Elaborate", Model, new ViewDataDictionary { {"vb", ViewBag}});} 

Partial:

@{    var vb = ((dynamic)ViewData["vb"]);    vb.TheTitle = "New values";  } 

Page

@ViewBag.TheTitle = "New value" 
like image 86
Tony Basallo Avatar answered Sep 21 '22 20:09

Tony Basallo