In my MVC 2 site, I have an html helper, that I use to add javascripts for my pages. In my master page I have the main javascripts I want to include, and then in the aspx pages, I include page specific javascripts.
So for example, my Site.Master has something like this:
....
<head>
<%=html.renderScripts() %>
</head>
...
//core scripts for main page
<%html.AddScript("/scripts/jquery.js") %>
<%html.AddScript("/scripts/myLib.js") %>
....
Then in the child aspx page, I may also want to include other scripts.
...
//the page specific script I want to use
<% html.AddScript("/scripts/register.aspx.js") %>
...
So when the full page gets rendered the javascript files are all collected and rendered in the head by sitemaster placeholder function RenderScripts. This works fine.
Now with MVC 3 and razor view engine, they layout pages behave differently, because now my page level javascripts are not rendered/included. Now all I see the LayoutMaster contents.
How do I get the solution wo workwith MVC 3 and the razor view engine. (The helper has already been re-written to return a HTMLString ;-))
For reference: my MasterLayout looks like this:
...
...
<head>
@{
Html.AddJavaScript("/Scripts/jQuery.js");
Html.AddJavaScript("/Scripts/myLib.js");
}
//Render scripts
@html.RenderScripts()
</head>
....
and the child page looks like this:
@{
Layout = "~/Views/Shared/MasterLayout.cshtml";
ViewBag.Title = "Child Page";
Html.AddJavaScript("/Scripts/register.aspx.js");
}
....
<div>some html </div>
Thanks for your help.
Edit = Just to explain, if this question is not clear enough.
When producing a “page” I collect all the javascript files the designers want to use, by using the html.addJavascript(“filename.js”) and store these in a dictionary – (1) stops people adding duplicate js files – then finally when the page is ready to render, I write out all the javascript files neatly in the header. (2) – this helper helps keep JS in one place, and prevents designers from adding javascript files all over the place. This used to work fine with Master/SiteMaster Pages in mvc 2. but how can I achieve this with razor?
Edit: html helper code:
....
private const string SCRIPTLINK =
"<script src=\"{0}\" type=\"text/javascript\"></script>";
public static HtmlString RenderScripts(this HtmlHelper helper) {
OrderedDictionary scripts =
helper.ViewContext.HttpContext.Items["JavaScripts"] as OrderedDictionary;
StringBuilder sb = new StringBuilder(500);
foreach (DictionaryEntry script in scripts) {
sb.AppendFormat(SCRIPTLINK, script.Key);
}
return new HtmlString(sb.ToString());
}
....
Thanks for the help and tips Slaks and David. I finally got a solution that seems to work, but with some things I don’t like. If there is a better way, please feel free to point it out.
Here is my Child Page:
I then have a _ViewStart.cshtml with:
And my MasterLayout looks like this:
This does render my script tags in the correct order and place, but does require the client to call the html helper renderScripts in child pages. which is ok, as we can create templates to help with that. Any better ideas?