Q:

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

I hava an editor template for, let's say, date:
@model DateTime
@section AdditionalJavaScript2
{
   /* some js code */
}

@Html.TextBox("", Model.ToString("d.M.yyyy"), new { @class = "date" }) 

Now, I would like to put some js code into the HEAD section, but this doesn't work.
Of course, I have a this section in my layout.cshtml:
<head>
  ...
  @RenderSection("AdditionalJavaScript2", required: false)
</head>

It works from the plain view, but not from partial view (editor template).
Why?
And, is there a workaround?
Thanks,
Igor

A:

A partial-view does not use a template, it returns "raw" html to be included in your page (by Javascript). It does not have access to anything but the stream it returns itself.
Think of it like this: You typically call a partial view from Javascript/AJAX to get some new html. You get the return, and replace some DIV-tag. How can the system (FireFox, Chrome, ...) know, that there is some extra section of data that needs to replace something in the HEAD tag.
There are some workarounds:

Don't put the script in the HEAD
Add a parameter switch betweed the html and the script. You need to client-side calls, one to get the html, and one for the script. You include the calls to the partial-view on two locations on your page. 
Separate the script and the html using some pre-defined tag like <!-- SEPERATOR -->, and let the calling code split the result, and put it in the correct position.

