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 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:
<!-- SEPERATOR -->, and let the calling code split the result, and put it in the correct position.