locked
jQuery - Get the correct path from partial view using ajax RRS feed

  • Question

  • User-78632608 posted

    Every one of my views can be an ajax view. Once the user clicks on an Ajax.Actionlink, it loads the ajax content, however the url doesn't get changed, so I am using History.js pushState() for this.

    Note: Html.BeginScriptContext() is just a helper extension to render my scripts.

    Here's what I was doing before: _RootPluginView:

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

    and in the partial view I called it like this: Index View:

    @{ Html.RenderPartial("_RootPlugin"); }

    updateHistory() would be called on ajax success and I would then use it in jquery to get the correct url path.

    function updateHistory() {
        var target = window.absolutePath;
        pushState(target);
    };
    
    function pushState(target) {
        manualStateChange = false;
        window.rootAction = target;
        History.pushState(null, window.title, target);
    }

    The problem with this is I have to put @{ Html.RenderPartial("_RootPlugin"); } into every single view to get the correct url (because every view can be loaded via ajax).

    Is there another way of doing this without tightly coupling my JS code to the view and/or having to put that rootPlugin partial into every view?

    Thursday, March 17, 2016 10:56 PM

Answers

  • User61956409 posted

    Hi Letmeregister,

    I can't do that because the layout view doesn't get called on Ajax requests. That's why I have this code in the partial views.

    In this case, you should put the code into each view, as you have done.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 21, 2016 9:00 AM

All replies

  • User61956409 posted

    Hi Letmeregister,

    The problem with this is I have to put 

    @{ Html.RenderPartial("_RootPlugin"); }

     into every single view to get the correct url (because every view can be loaded via ajax).

    Is there another way of doing this without tightly coupling my JS code to the view and/or having to put that rootPlugin partial into every view?

    If you are using a layout or master page, you could try to put @{ Html.RenderPartial("_RootPlugin"); } in layout or master page.

    Best Regards,

    Fei Han

    Friday, March 18, 2016 7:06 AM
  • User-78632608 posted

    I can't do that because the layout view doesn't get called on Ajax requests. That's why I have this code in the partial views.

    Friday, March 18, 2016 5:18 PM
  • User61956409 posted

    Hi Letmeregister,

    I can't do that because the layout view doesn't get called on Ajax requests. That's why I have this code in the partial views.

    In this case, you should put the code into each view, as you have done.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 21, 2016 9:00 AM