-->

PJax不与MVC项目工作(PJax not working with MVC project)

2019-10-18 18:37发布

我已经按照样品。 我添加了一个_PjaxLayout:

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

修改我的_layout:

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

更新ViewStart:

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

然而,我每次点击一个“A”的标签时,pjax代码不会被调用。 就好像当我设置了pjax选择不工作。 我究竟做错了什么?

更新:

如果我这样做:

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

我看到pjax代码击中和请求头得到更新,并在页面上的新内容加载,但造型和布局回真的混淆和重复...

更新:

检查这种疯狂情况发生后的DOM揭示了新的一页的内容获得直接加载到我点击,而不是与ID #shell元素的锚点。 WTF?

Answer 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";
  }
}


Answer 2:

我没有看到那个语法PJax文档中有效。

你确定你不是故意$(document).pjax('a',{});

$.pjax立即从我可以告诉执行。



文章来源: PJax not working with MVC project
标签: pjax