0
  • I have a partial view which contains javascript specific only to said view
  • Apparently, ASP.NET MVC does not support sections in partial views, so I cannot add a script section to my partial to be added to the end of my layout page
  • I came upon this question which proposes a promising-looking set of extensions, but it requires that I put the javascript in a separate .js file.
  • So I did that only to find that the Razor syntax in the original javascript won't work there.
  • I then happened upon a VS plugin called RazorJS, but it doesn't appear to have been updated since 2011, so I am not even going to give it a go.

So it appears my only option is to host my javascript in a view, but I need to be able to inject it into the view from a partial view. Any way to do this?

Community
  • 1
  • 1
oscilatingcretin
  • 10,457
  • 39
  • 119
  • 206
  • Let's make sure I understand this - you want your javascript to be rendered by Razor, correct? So you can do something like this I assume: `var myVal = '@Model.MyVal'; – Matt Spinks Jan 26 '17 at 16:28
  • @MattSpinks That's basically it, yes, but I have to be able to do this in a separate .js file – oscilatingcretin Jan 26 '17 at 16:30
  • There is no way to render anything from Razor into a separate javascript file (at-least not without a plugin). What is commonly done, however, is to set variables' values within your main view page, and then in your separate javascript file, consume those variables/values via parameters. – Matt Spinks Jan 26 '17 at 16:46
  • Why does the js "have to be" in a `@section` (of partial)? If it seems to be specific to it(?). Can the script be "cut up" - e.g. create an object that's dependent on some `model` on the Partial, and the "rest" of the script (the logic that works with the `object) separately? – EdSF Jan 26 '17 at 16:47
  • @MattSpinks That is actually what I ended up doing as a work-around (declared the js variable in my layout before the other scripts were loaded). It works, but now I have stuff in my layout page that only applies to certain views, so I want to get it out of there if at all possible – oscilatingcretin Jan 26 '17 at 17:17
  • @EdSF I have a "script" section at the bottom of my layout. From within my partial, I want to be able to tell the parser that I have scripts pertenant to this partial, but want them loaded at the very end of the rendered document. – oscilatingcretin Jan 26 '17 at 17:19
  • Ok so, the "functional" part of the script can be in the `@section` of the container `view` (so that gets "restricted" to the `view` that uses the `partial` and renders in the right place), and the "data" part of the script that depends on some `model` goes with the `partial`. Does that work? – EdSF Jan 26 '17 at 19:02
  • Scripts should never be in partials. Apart from the fact they will be rendered in line, you risk rendering duplicates. Put the script in the main view of layout. –  Jan 27 '17 at 02:10

1 Answers1

-2

There is no way to render anything from Razor into a separate javascript file (at-least not without a plugin). What is commonly done, however, is to set variables' values within your main view page, and then in your separate javascript file, consume those variables/values via parameters.

For example...

View:

<html>
<body>
...
  <button onclick='myFunction("@Model.MyVal");'>Click Here!</button>
</body>
</html>

Javascript file:

function myFunction(myClickedVal) {
  alert(myClickedVal);
}

You could also use the same concept and use global variables. But do so at your own risk.

Matt Spinks
  • 6,380
  • 3
  • 28
  • 47