0

My whole website is an Ajax website because I have a footer music player.

I would like to add:

@using (Html.BeginScriptContext())
{
    Html.AddScriptBlock(
        @<script>
                @if (Request.IsAjaxRequest())
                {
                    <text>
            window.rootQuery = '@Request.Url.Query';
            window.rootAction = '@Url.Action("/")';
            window.absolutePath = '@Request.Url.AbsolutePath';
        </text>
            }
    </script>, 
true);
}

To every page without having to go into my 30+ views and manually do it. I can't just add this to Layout.cshtml because Layout isn't called when you return a partialViewResult which is what my website is doing 90% of the time. Any way to do this?

2
  • Wouldn't have this problem if you didn't mix javascript with server code. It's called "separation of concerns" . Commented Feb 15, 2016 at 2:52
  • 1
    I think you should add the main script to your layout as a function, then call that function in all the 30+ views with different params Commented Feb 15, 2016 at 7:34

1 Answer 1

1

One way to do it would be adding this code to a partial view and rendering this partial view to the pages you want to. Still you would manually need to add a line to all the pages. Something like:

@Html.RenderPartial("MyPlugin", Model.PluginData);

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.