Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# ASP.NET MVC
Hi Guys,
Quick question, in webforms, you extend your i mean
 public class MyPage : Page
and you can write to your page header using
 protected override void OnPreRender(EventArgs e)
  var jqueryitems = new Dictionary<string, string>
                                      {"6", "jquery.common.js"},
                                      {"7", "jquery.autocomplete.js"},
                                         {"8", "jquery.autocomplete.common.js"},
                                      {"9", "json2.js"}
            foreach (KeyValuePair<string, string> pair in jqueryitems)
                var commonJquery = new HtmlGenericControl("script");
                commonJquery.Attributes.Add("type", "text/javascript");
                commonJquery.Attributes.Add("language", "javascript");
                commonJquery.Attributes.Add("src", string.Format("/common/home/client/jQuery/{0}", pair.Value));
                this.Header.Controls.AddAt(int.Parse(pair.Key), commonJquery);
In mvc how would you go about doing the same technique,but this time using BaseControler or controler class?
Posted 23-Apr-13 22:42pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The simplest way I can imagine right now is to use a combination of sections and ViewBag. You can use the sections (see:[^]) to make a header section in the layout. Than you can add a dictionary like you mentioned (or something similar) to ViewBag to store (gather) script reference needs during execution of the actions. And you can use the data gathered in that object to render in the view the necessary script references into the header section.
You can make a helper class to have related tasks organized tidy.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 495
1 Sergey Alexandrovich Kryukov 469
2 OriginalGriff 360
3 Sascha Lefévre 255
4 Abhinav S 250
0 Sergey Alexandrovich Kryukov 10,102
1 OriginalGriff 9,495
2 Peter Leow 5,241
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 3,076

Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 24 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100