如何在pagedList中使用ajax

时间:2014-12-23 08:34:53

标签: c# jquery ajax razor pagedlist

我正在使用分页列表并在我点击下一个工作时调用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);           
}

这是局部视图

1 个答案:

答案 0 :(得分:3)

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

并将patrial视图代码更改为此

@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>