When in the Editor Template, how can I put javascript in the chapter section?

I hava editor template, let's say date:

@model DateTime @section AdditionalJavaScript2 { /* some js code */ } @Html.TextBox("", Model.ToString("dMyyyy"), new { @class = "date" }) 

Now I would like to put some js code in the HEAD section, but this will not work.

Of course, I have this section in my layout.cshtml:

 <head> ... @RenderSection("AdditionalJavaScript2", required: false) </head> 

It works from a simple view, but not from a partial view (editor template).

Why?

And is there a workaround?

Thanks,

Igor

+4
source share
1 answer

Partial viewing does not use a template, it returns a "raw" html for inclusion in your page (by Javascript). He has no access to anything except the stream, which he returns.

Think of it this way: you usually call a partial view from Javascript / AJAX to get a new html. You will receive a refund and replace the DIV tag. How the system (FireFox, Chrome, ...) knows that there is some additional data section that should replace something in the HEAD tag.

There are some workarounds:

  • Do not put script in HEAD
  • Add a parameter switcher between html and script. You need client calls, one for receiving html and one for script. You enable partial view calls in two places on your page.
  • Separate the script and html with some predefined tag, such as <!-- SEPERATOR --> , and call the calling code to split the result and put it in the correct position.
+2
source

All Articles