1

I want to be able to able to register script blocks in the ViewData or ViewBag and then unload them on my layout page at the correct position.

I tried writing a @function {} in my _Layout but this cannot be called from my Views.

So how do I write a class that allows me to do something similar to

@Something.registerscript("myscript.js")

And then on the Layout page

@Something.RenderScripts()

I saw an implementation using the singleton pattern here...

Add CSS or JavaScript files to layout head from views or partial views

But im afraid that will cause problems as this should be dynamic not static!

Community
  • 1
  • 1
Exitos
  • 29,230
  • 38
  • 123
  • 178

3 Answers3

0

Is this for a generic solution or do you just want to include scripts files from within your views? For the latter, you can always create a section called "Head" or whatever which is created within the <head>-element.

For example:

_layout.cshtml
<html>
     <head>
          @RenderSection("Head", false)
     </head>
     ...
</html>

View.cshtml
@section Head
{
    <script type="text/javascript">...</script>
}

I wouldn't propose this as an answer, since there is the possiblity that you do want to actually do more than this (as far as I know, this doesn't work with EditorTemplates etc.). But in case you were thinking to complicated, this works very easy.

J. Tihon
  • 4,439
  • 24
  • 19
0

Why would you use ViewBag for this ? If you want to create something that has request-wide scope, use HttpContext.Current.Items - in your case implement one storing helper method, and one "render everything stored" method.

rouen
  • 5,003
  • 2
  • 25
  • 48
  • why not use viewbag? Im only passing data from a view up to _layout then rendering page? In addition why dont you provide a sample rather than just pick holes in my solution? – Exitos Nov 20 '11 at 18:33
  • why not? because the scope is wrong i think. For example, i am not sure about ViewBag scope in case of multiple parts generated by RenderAction.. Items is always solid 1 request scope. And you really need me to provide sample of "store this into dictionary", "get this from dicrionary" wrapper ? I am quite surprised that someone here reacts this way ("dont pick holes in my solution") - i am trying to help you man – rouen Nov 20 '11 at 18:48
  • I dont really see how I can pass the Items out to a method, thats what im struggling with, shall I just pass it by reference? Sorry just had a day of it..., I see what your saying about the scope this is handy information... – Exitos Nov 20 '11 at 19:03
  • HttpContext.Current.Items - you can use it this way on any place, in controller, in Html helper (extension method), etc. – rouen Nov 20 '11 at 21:03
0

Viewbag or it's close relative viewdata I'd say are the best collections to use for this, as your context appears to be within the view. Where would you register the script & where would you render it?

You could, I think, write helper extensions to wrap up the register & render side of things. I believe helper methods can access viewdata. You'd get the added bonus of the abstraction away from the actual store you used, as you'd only reference it from these helper methods.

Simon Halsey
  • 5,459
  • 1
  • 21
  • 32