【问题标题】:Ajax paging is duplicating _layout page while using pagination to replace page contentAjax 分页在使用分页替换页面内容时复制 _layout 页面
【发布时间】:2018-01-19 00:04:43
【问题描述】:

我正在使用x.PagedList 在我的 ASP.NET MVC 页面中使用分页。我对插件的唯一问题是,当我在页面之间导航时它使用了页面刷新。

为了避免我使用 jQuery 调用来替换页面内容,如 article 中所述。

我的视图和 javascript 看起来像这样。

<div id="circuitsContent">
    <table class="table">
        <tr>
            --Header 
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                --Loop through and create content
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
                    @Html.ActionLink("Details", "Details", new { id = item.ID }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.ID })
                </td>
            </tr>
        }

    </table>
</div>

<div id="circuitContentPager">
    @Html.PagedListPager((IPagedList)Model, page => Url.Action("Circuits", new { page }))
</div>

@section scripts
{
    <script language="javascript" type="text/javascript">
        $(document).ready(function () {
            $(document).on("click", "#circuitContentPager a[href]", function () {
                $.ajax({
                    url: $(this).attr("href"),
                    type: 'GET',
                    cache: false,
                    success: function (result) {
                        $('#circuitsContent').html(result);
                    }
                });
                return false;
            });
        });
 </script>

这是我的控制器代码:

public ActionResult Circuits(int? page)
        {

            var pageNumber = page ?? 1;
            var circuits = _repo.GetAllCircuits().OrderBy(circ=>circ.ID).ToList();
            var pagedCircuits = circuits.ToPagedList(pageNumber, 25);


            return View(pagedCircuits);
        }

我在这里错过了什么?

【问题讨论】:

  • 首先,您应该返回PartialViewResult,而不是ViewResult
  • 您的代码还有其他问题 - 您需要使用不同的方法来返回结果(与呈现初始视图的方法不同),以便部分仅呈现表格的 html .

标签: javascript jquery asp.net ajax asp.net-mvc


【解决方案1】:

您的 ajax 调用从 Circuits() 方法返回 html,这与您最初用于呈现页面的视图相同,其中包括所有初始 html,但您只替换了现有页面的一部分,因此元素如@Html.PagedListPager() 方法生成的分页按钮将被重复。由于重复的 id 属性,您还会生成无效的 html(您将有多个 &lt;div id="circuitsContent"&gt; 元素

有两种方法可以解决这个问题。

创建一个单独的控制器方法,该方法仅返回 &lt;table&gt; 的部分视图并调用该方法,但是您需要提取寻呼机按钮的 href 属性的页码值以将其传递为好吧。

使用您当前的 Circuits() 方法,测试请求是否为 ajax,如果是,则仅返回 &lt;table&gt; 的部分视图。

public ActionResult Circuits(int? page)
{
    var pageNumber = page ?? 1;
    var circuits = _repo.GetAllCircuits().OrderBy(circ=>circ.ID);
    var pagedCircuits = circuits.ToPagedList(pageNumber, 25);
    if (Request.IsAjaxRequest)
    {
        return PartialView("_Circuits", pagedCircuits);
    }
    return View(pagedCircuits);
}

注意:不要在查询中使用.ToList()。这违背了使用服务器端分页的全部目的,因为.ToList() 会立即从数据库中下载所有记录。

_Circuits.cshtml 在哪里

@model IEnumerable<yourModel>
<table class="table">
    <thead>
        <tr>
            // <th> elements
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
            .... // Loop through and create content
            </tr>
        }
    </tbody>
</table>

请注意,您的标题元素应位于 &lt;thead&gt; 元素中,而记录应位于 &lt;tbody&gt; 元素中。

【讨论】:

  • 这可行,但现在活动页面不会更改为新页面。即使我将导航更改为第 4 页,活动页面仍保留在第 1 页!更新了问题的新值...
  • 不确定我是否理解。您的意思是当您单击“第 6 页”按钮时,它只返回第 1 页的结果? (如果是这样,请检查您是否在控制器方法中实际获得了 page 参数的值)
  • 好的,刚刚看到您的编辑。您需要一些 javascript 来设置您刚刚单击的按钮的相关类名(抱歉,我不记得那是什么,但如果您检查页面源,我相信您会找到它)
  • @Simsons,我回滚了您的编辑,因为它与您的问题无关(您只会得到这种行为,因为您实施了我的答案,因此在问题的上下文中没有任何意义)。只是一个猜测,但我认为它会是active,所以你需要$(document).on("click", "#circuitContentPager a[href]", function () { $(this).addClass('active') ...,但你还需要跟踪当前按钮,所以当你点击另一个按钮时,你可以从中删除该类(我不'这个设备上没有安装PagedList,所以现在不能测试它)
  • 我尝试在您发表第一条评论后添加活动课程,但它不起作用。目前所有的按钮都是 as , li 并且激活的是 ,
  • 1
  • 没有激活的是
  • 4
  • 。所以试图找到父母并添加课程。此外,正如您正确提到的,还需要跟踪这些...
猜你喜欢
相关资源
最近更新 更多
热门标签