【问题标题】:How to use ajax in pagedList如何在pagedList中使用ajax
【发布时间】:2015-02-21 09:08:56
【问题描述】:

我正在使用分页列表并在单击下一个有效时调用 ajax。问题是当我单击不调用 ajax 的上一个或页码时。我需要在分页列表中使用 ajax 调用而不是我自己的ajax。

public ActionResult ApplicantsRecord(int? page)
{
        List<ApplicantsRecord> ar = new List<ApplicantsRecord>();
        ApplicantsRecord a = new ApplicantsRecord();
        List<ApplicantsRecordDetailViewModel> apvmlist = new List<ApplicantsRecordDetailViewModel>();
        ApplicantsRecordDetailViewModel apvm = new ApplicantsRecordDetailViewModel();
        //ar = db.ApplicantsRecords.ToList();
        var groupedAR = db.ApplicantsRecords.GroupBy(x => x.SessionId)
                                .Select(y => new
                                {
                                    SessionId = y.Key,
                                    ApplicationsRecords = y.FirstOrDefault(),
                                }).ToList().OrderByDescending(x => x.ApplicationsRecords.LoginDate);

        foreach (var i in groupedAR)
        {
            ar.Add(i.ApplicationsRecords);
        }
        int pageNumber = (page ?? 1);          
        if(Request.IsAjaxRequest())
        {
            return PartialView("_ApplicantsRecord", ar.ToPagedList(pageNumber, 10));
        }
        return View(ar.ToPagedList(pageNumber, 10));
}

查看代码

<div id="targetContainer">
   @Html.Partial("_ApplicantsRecord",Model);
</div>

ajax 代码

var npage =2;
$(document).ready(function () {
$('#container').click(function () {
            $.ajax({
                type: "GET",
                traditional: true,
                cache: false,
                url: '/Testing/ApplicantsRecord/',
                data:{page:npage}                    
            })
            .success(function (html) {
                UpdatePage(html);                   
                })
            .error(function () {                   
            });
            return false;
      });
  });
function UpdatePage(html) {
   $("#targetContainer").empty().html(html);
   newpage = $('#newpage').val();      
   npage = parseInt(npage)
   npage = npage + 1;
   $('#newpage').val(npage);           
}

这是局部视图

【问题讨论】:

  • 您是否从控制台收到任何错误?
  • 不,我没有收到任何错误它的工作我需要更多的东西
  • 您的 ajax 请求是否总是使用 page:2 调用?
  • 所以对吗?你能记录下来吗?通话在哪里中断?
  • 问题是当我点击任何分页列表按钮时,它会给我一个下一页

标签: c# jquery ajax razor pagedlist


【解决方案1】:

我知道了,我没有在我的 jquery 包中使用 jquery unobtrusive 我使用 Nuget 包下载了 jquery unobtrusive ui 并添加到一个新包中 然后将该包包含在我的 _Layout 视图中,而不是它开始工作的 jquery 包中

并将父视图代码改成这个

@model IPagedList<ApplicantsRecord>


    <div id="container">
        <div class="pagedList">
            @Html.PagedListPager(Model, page => Url.Action("ApplicantsRecord", new { page = page }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions()
   {
       HttpMethod = "GET",
       InsertionMode = InsertionMode.Replace,
       UpdateTargetId = "targetContainer"

   }))
        </div>
    </div>
    <table>
        <thead>
            <tr>
                <th width="200" align="center">User Name</th>
                <th width="200" align="center">Login Date Time</th>
                <th width="100" align="center">Details</th>

        </tr>
    </thead>
    <tbody>
        @foreach (var group in Model.GroupBy(x => x.UserName))
        {
            <tr class="group-header">
                <td colspan="6">
                    <span class="label label-info">Applicant : @group.Key</span>
                    <span class="label label-success">Total Test Taken: @group.Count()</span>
                </td>

            </tr>
            foreach (var item in group)
            {
                <tr>
                    <td>@Html.DisplayFor(modelItem => item.UserName)</td>
                    <td>@Html.DisplayFor(modelItem => item.LoginDate)</td>
                    <td>@Html.ActionLink("Details", "ApplicantsRecordDetail", new { id = item.SessionId })</td>

                </tr>

            }
        }
</table>

以及查看到这个的代码

@model IPagedList<ApplicantsRecord>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "ApplicantsRecord";
}

<h2>ApplicantsRecord</h2>
<p>
    @Html.ActionLink("Back To List", "QuestionDetail") |
    @Html.ActionLink("Live View ", "LiveView")

</p>
    <div id="targetContainer">
       @Html.Partial("_ApplicantsRecord",Model)
    </div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 2019-10-27
    • 1970-01-01
    相关资源
    最近更新 更多