Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
135
rated 0 times [  137] [ 2]  / answers: 1 / hits: 15908  / 11 Years ago, mon, february 17, 2014, 12:00:00

We are making a .Net MVC websolution that is going to be using widgets rendered as HTML.Partials(). We would like to be able for the partial view to add its dependencys in the main page views scripts tags, both css-files as well as javascript files.



Ive been trying with this code in my partial. But the js-file isnt rendered in the layout section of my page. What is wrong?



@{

var bundle = System.Web.Optimization.BundleTable.Bundles.GetRegisteredBundles()
.Where(b => b.Path == ~/bundles/jquery)
.First();

bundle.Include(~/Scripts/addtojquerybundletest.js);
}

More From » css

 Answers
0

It just seemed to be specific to my jquery bundle. It now works, I have added a widgetspecific bundle where you can add scripts to render in the main layout. The bundle also handles the adding of the same file several times (which can happen if the same widget exists several times on the same page) and only renderes it once. My solution added below, which doesnt seem to be that well documented from the searches Ive done so far.



In BundleConfig.cs:



bundles.Add(new ScriptBundle(~/widgetspecific));


In _Layout.cshmtl:



@Scripts.Render(~/widgetspecific)


In Widget.cshtml:



@{
var bundle = System.Web.Optimization.BundleTable.Bundles.GetBundleFor(~/widgetspecific);

bundle.Include(~/Scripts/andreas.js);
}


Is anyone aware of negative aspects to this solution?


[#72486] Saturday, February 15, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ronniem

Total Points: 584
Total Questions: 111
Total Answers: 111

Location: Finland
Member since Sat, Nov 6, 2021
3 Years ago
ronniem questions
Sat, Apr 24, 21, 00:00, 3 Years ago
Wed, Mar 10, 21, 00:00, 3 Years ago
Sat, Feb 6, 21, 00:00, 3 Years ago
;