PJax not working with MVC project

我只是一个虾纸丫 提交于 2019-12-11 09:23:24

问题


I've followed the samples. I added a _PjaxLayout:

<title>@ViewBag.Title</title>
@RenderBody()

Modified my _Layout:

    <div id="shell">
        @RenderBody()
    </div>
    <script type="text/javascript">
    $(function () {
        // pjax
        $.pjax.defaults.timeout = 5000;
        $('a').pjax('#shell');
    })
    </script>

Updated ViewStart:

  @{
  if (Request.Headers["X-PJAX"] != null) {
    Layout = "~/Views/Shared/_PjaxLayout.cshtml";
  } else {
    Layout = "~/Views/Shared/_Layout.cshtml";
  }
}

Yet every time I click on an 'a' tag, the pjax code doesn't get called. It's as if the selector isn't working when I set up pjax. What am I doing wrong?

UPDATE:

If I do this:

        $('document').ready(function () {
        $('a').pjax({
            container: '#shell',
            timeout: 5000
        });
    });

I see the pjax code getting hit and the Request headers get updated, and the new content loads on the page, but the styling and layout get really messed up and duplicated...

UPDATE:

Inspecting the DOM after this craziness happens reveals that the new page content is getting loaded directly into the anchor that I click, instead of into the element with id #shell. WTF?


回答1:


You are using a legacy syntax, the new pjax uses the following:

$(document).pjax('a', '#shell', { fragment: '#shell' });

Also I am not familiar with the language you use, but in order to make pjax happen there has to be an HTML element with the id shell in your ViewStart.

As I am not sure about the syntax in that language, try something similar to this for testing:

 @{
  if (Request.Headers["X-PJAX"] != null) {
    echo "<ul id="shell"> pjaaxxx </ul>"; // Would work in php, update syntax 
  } else {
    Layout = "~/Views/Shared/_Layout.cshtml";
  }
}



回答2:


I am not seeing that syntax as valid in the PJax documentation.

Are you sure you didn't mean $(document).pjax('a',{});?

$.pjax immediately executes from what I can tell.



来源:https://stackoverflow.com/questions/19306116/pjax-not-working-with-mvc-project

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!