Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET MVC
Hi Guys,
 
Quick question, in asp.net webforms, you extend your system.web.ui.page 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?
Paige
Posted 23-Apr-13 22:42pm
Mninawa1.3K

1 solution

Rate this: bad
good
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: http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvc3razor_topic2.aspx[^]) 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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 195
1 Afzaal Ahmad Zeeshan 132
2 ProgramFOX 130
3 Maciej Los 105
4 Sergey Alexandrovich Kryukov 75
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 24 Apr 2013
Copyright © CodeProject, 1999-2014
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