Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3958034
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:33:46+00:00 2026-05-20T02:33:46+00:00

I am using MVC 3 with the Razor view engine. I want to set

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T02:33:47+00:00Added an answer on May 20, 2026 at 2:33 am

    I also had this problem, and couldn’t find any neat and obvious solution.

    The solution I came up with was to implement an Html extension method that returns a ‘PageData’ class that you define, containing whatever data you need:

        [ThreadStatic]
        private static ControllerBase pageDataController;
        [ThreadStatic]
        private static PageData pageData;
    
        public static PageData GetPageData(this HtmlHelper html) {
            ControllerBase controller = html.ViewContext.Controller;
            while (controller.ControllerContext.IsChildAction) {
                controller = controller.ControllerContext.ParentActionViewContext.Controller;
            }
            if (pageDataController == controller) {
                return pageData;
            } else {
                pageDataController = controller;
                pageData = new PageData();
                return pageData;
            }
        }
    

    It finds the top-level controller for the current request, and returns the same PageData object every time the method is called within the same HTTP request. It creates a new PageData object the first time it is called in a new HTTP request.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using MVC 2 and the default view engine to return .ascx partial views
I am using ASP.NET MVC 3 with the razor view engine. I have the
I am using Razor view engine in ASP.Net MVC 3 RC 2. This is
First of all I'm using MVC 3 RC1 with the Razor view engine. I've
Using the new ASP.NET MVC 3.0 Razor View Engine, is there any way to
Is there any documentation on how to use the Razor view engine using ASP.NET
How can i directly access a .cshtml file in ASP.NET MVC using razor view
Is it possible to use Razor View Engine (ASP.NET MVC) together with .LESS (similar
Using ASP.NET MVC's default view engine, you can declare a server-side comment like this:
I'm using MVC to validate some html text boxes on a page, for example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.