【问题标题】:Working with mvc partial view and history.js使用 mvc 局部视图和 history.js
【发布时间】:2014-05-10 15:39:32
【问题描述】:

我正在使用历史 js,https://github.com/browserstate/history.js/ 和 Mvc 部分视图,这是我的代码

在部分首页

<a id="CreateUser" class="btn btn-primary">Create User </a>

在我的@section 脚本中推送新的 url 并在“状态更改事件”中加载部分内容

$('#back').click(function (e) {
    History.pushState({ state: 1 }, "Create User", "/System/User/Create/");
});

在控制器中还有逻辑加载部分视图如果它的ajax调用和完整视图如果没有,这是为了保护用户如果他们在~/System/User/Create/中按下刷新。 无论如何,我的脚本 onClick 脚本在部分中不起作用 /System/User 中的用户从部分视图或 Ajax 调用,我可以通过在 @section 脚本之外附加 OnClick 事件来解决这个问题,所以它变成了这样:

<a id="CreateUser" class="btn btn-primary">Create User </a>
<script>
        $('#back').click(function (e) {
           History.pushState({ state: 1 }, "Create User", "/System/User/Create/");
        });
</script>

@section scripts
{
<script>
    $('#back').click(function (e) {
           History.pushState({ state: 1 }, "Create User", "/System/User/Create/");
    });
</script>
}

但这是多余的,维护起来很痛苦我知道这是因为@section 不适用于 Ajax/Partial,但除此之外还有更清洁的替代方案。

【问题讨论】:

    标签: c# asp.net-mvc history.js


    【解决方案1】:

    没关系在加载 ajax 后发现 '.click' 需要重新绑定,但是使用 .on('click' 不需要重新绑定事件处理程序,所以我现在只需要全局 js 中单击事件的脚本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      相关资源
      最近更新 更多