I used this answer to delay rendering of scripts in partials/editors: https://stackoverflow.com/a/14127332/1612788
This relies on enclosing the desired scripts in a
@using (Html.BeginScripts())
{
<script>
var foo = "Bar";
</script>
}
This works great when the partial is loaded synchronously with the rest of the page. However, when the partial is called from an ajax request, it doesn't render the script in the response.
Is there a way that I can tell it to use Html.BeginScripts
when it's a synchronous request, and ignore Html.BeginScripts
when it's an asynchronous request? In other words, I just want to write the script out to the response as normal when the partial is requested by ajax.
I could probably do something like:
@if (Request.IsAjaxRequest())
{
<script>
var copy = "Bar";
</script>
}
else
{
using (Html.BeginScripts())
{
<script>
var copy = "Bar";
</script>
}
}
But is there a way this can be done without having two copies of the script in the source?