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

  • SEARCH
  • Home
  • 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 7822071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:49:23+00:00 2026-06-02T07:49:23+00:00

Update : It seems I was trying to set the ViewBag content in a

  • 0

Update: It seems I was trying to set the ViewBag content in a partial view instead in a plain view, MVC3 makes it very easy to do this. I’m still interested why it doesn’t work for partial views, i.e. why isn’t the viewbag shared with partial views.

I’m trying to pass some data from a partial view to the layout view by using ViewData/ViewBag

// Used in the partial view
public static void RequireAssets(this HtmlHelper helper, params Asset[] assets)
{
    var alreadyRequired = helper.ViewData["RequiredAssets"] as List<Asset>;
    if (alreadyRequired == null)
    {
        alreadyRequired = new List<Asset>();            
        helper.ViewData.Add("RequiredAssets", alreadyRequired);
    }

    foreach (var asset in assets.Where(anAsset => !alreadyRequired.Contains(anAsset)))
        alreadyRequired.Add(asset);
}

// Used in the layout view
public static MvcHtmlString RenderAssetStyles(this HtmlHelper helper)
{
    var requiredAssets = helper.ViewData["RequiredAssets"] as List<Asset>;
    return requiredAssets == null ? null : GetStyleSheets(requiredAssets.Select(e => new StyleSheet(e)).ToArray());
}

public static MvcHtmlString RenderAssetScripts(this HtmlHelper helper)
{
    var requiredAssets = helper.ViewData["RequiredAssets"] as List<Asset>;
    return requiredAssets == null ? null : GetScripts(requiredAssets.Select(e => new Script(e)).ToArray());
}

The problem is that when RenderAssetStyles/Scripts is fired in the layout view the ViewData does not contain the key “RequiredAssets”

Usage

_Layout.cshtml:

<title>@ViewBag.Title</title>    
@Html.RenderAssetStyles()

PartialView.cshtml:

@{ 
    Html.RequireAssets(Assets.Grid, Assets.FileUpload); 
}
  • 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-06-02T07:49:23+00:00Added an answer on June 2, 2026 at 7:49 am

    I could not reproduce this. I had to simplify your code and make some assumptions. But the following worked fine for me.

    _Layout.cshtml

    @using MyProject.Helpers
    <title>@ViewBag.Title</title>    
    @Html.RenderAssetStyles()
    @Html.RenderAssetScripts()
    @RenderBody()
    

    View.cshtml

    @using MyProject.Helpers
    @{ 
        ViewBag.Title = "View";
        Html.RequireAssets(Asset.Grid, Asset.FileUpload);
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    

    Helpers.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace MyProject.Helpers
    {
        public enum Asset
        {
            Grid=1,
            FileUpload=2
        }
        public static class Helper
        {
    
            public static void RequireAssets(this HtmlHelper helper, params Asset[] assets)
            {
                var alreadyRequired = helper.ViewData["RequiredAssets"] as List<Asset>;
                if (alreadyRequired == null)
                {
                    alreadyRequired = new List<Asset>();
                    helper.ViewData.Add("RequiredAssets", alreadyRequired);
                }
    
                foreach (var asset in assets.Where(anAsset => !alreadyRequired.Contains(anAsset)))
                    alreadyRequired.Add(asset);
            }
    
            // Used in the layout view
            public static MvcHtmlString RenderAssetStyles(this HtmlHelper helper)
            {
                var requiredAssets = helper.ViewData["RequiredAssets"] as List<Asset>;
                return requiredAssets == null ? null : MvcHtmlString.Create("Test Style");
            }
    
            public static MvcHtmlString RenderAssetScripts(this HtmlHelper helper)
            {
                var requiredAssets = helper.ViewData["RequiredAssets"] as List<Asset>;
                return requiredAssets == null ? null : MvcHtmlString.Create("Test Script");
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to execute this query with PDO: UPDATE categories SET keywords =
I'm trying to do something like this in postgres: UPDATE table1 SET (col1, col2)
Am trying to perform an update and nothing seems to work. It has something
Update: This is, as I was told, no principle Python related problem, but seems
I'm trying to set up a view that gives feed back via progress bar
I am trying a simple UPDATE table SET column1 = 0 on a table
I am trying to set the Django Admin to display English. This is what
I'm trying to add a link to a Rails view to set a boolean
Update I missed this in my original explanation. I set this up yesterday, and
I'm trying to set up a simple association in NHibernate (this is the first

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.