Mvcpaging Ajax模式无法正常工作

时间:2013-10-20 06:46:30

标签: jquery ajax paging

尝试让MvcPaging在Ajax模式下工作,但它没有合作!它可以工作,但完全GET而不是Ajax调用。当我进入控制器代码时,Request.IsAjaxRequest()为false,表明它无法正常工作。下面是我的剃刀视图和控制器代码:

@using MvcPaging
@model IPagedList<PracWare.Net.patientnote>

    @Scripts.Render("~/bundles/jquery")
<link href="~/Content/paging.css" rel="stylesheet" />
<link href="~/Content/Bootstrap2.3.1.css" rel="stylesheet" />
<div id="notes-list">
        <table width="98%">
            <tr><th>Date</th><th>Notes</th></tr>
            @foreach (var note in Model)
            {
                <tr><td>@note.dateTime</td><td>@note.notes</td></tr>
            }
        </table>

<div id="paging-control">
    @Html.Raw(Ajax.Pager(
        new Options
            {
                PageSize = 5,
                TotalItemCount = Model.TotalItemCount,
                CurrentPage = Model.PageNumber,
                ItemTexts = new ItemTexts() { Next = "Next", Previous = "Previous", Page = "P" },
                ItemIcon = new ItemIcon() { First = "icon-backward", Previous = "icon-chevron-left", Next = "icon-chevron-right", Last = "icon-forward" },
                TooltipTitles = new TooltipTitles() { Next = "Next page", Previous = "Previous page", Page = "Go to page {0}.", First = "Go To First Page", Last = "Go To Last Page" },
                Size = Size.normal,
                Alignment = Alignment.centered,
                IsShowControls = true,
                IsShowFirstLast = true,
            },
        new AjaxOptions
            {
                UpdateTargetId = "notes-list",
                OnBegin = "beginPaging",
                OnSuccess = "successPaging",
                OnFailure = "failurePaging"
            }, new { controller = "Patient", action = "JqAjaxOrders"}))
    </div>
</div>

控制器代码:

public ActionResult JqAjaxOrders(int? page)
{
     var db = new PracwareEntities();
     var orders = db.patientnotes.OrderBy(o=>o.dateTime).ToPagedList(page ?? 1, 5);
        if (Request.IsAjaxRequest())
            return PartialView("JqAjaxOrders", orders);
        return View(orders);
}

我直接从MvcPagingDemo解决方案中获取此代码,该解决方案可以正确执行Ajax调用。我可能错过了什么?

1 个答案:

答案 0 :(得分:0)

我确定MvcPaging很棒,但我花了2个小时对着屏幕抨击我无法让它工作,所以我编写了自己的实现......这花了不到2个小时!

控制器代码:

   [Authorize]
    public ActionResult ListNotes(int patientId, int? page = 1)
    {
        const int pageSize = 5;
        var db = new PracwareEntities();
        var totalNotes = db.patientnotes.Count(n => n.patientDbId == patientId);
        var notes = db.patientnotes.OrderBy(o => o.dateTime).Skip((int)((page - 1) * pageSize)).Take(pageSize).Where(n => n.patientDbId == patientId).ToList();

        ViewBag.PageCount = GetPageCount(totalNotes, pageSize);
        ViewBag.CurrentPage = page;
        ViewBag.patientId = patientId;
        if (Request.IsAjaxRequest())
            return PartialView("ListNotes", notes);
        return View(notes);
    }

    private static int GetPageCount(int notesCount, int pageSize)
    {
        var x = (double) notesCount/pageSize;
        if (x - (int) x > 0)
            return (notesCount/pageSize) + 1;
        return (notesCount/pageSize);
    }

Razor观点:

@if (ViewBag.PageCount > 1)
{
<div id="paging-control" class="pagination ">
    <ul>
        <li class="first">
            @Html.ActionLink("First", "ListNotes", "Patient", 
                             new {ViewBag.patientId, Page = "1" }, new {@Class="update-notes-list "} )
        </li>
        @for (var i = 1; i <= ViewBag.PageCount; i++)
        {
            <li @if (ViewBag.CurrentPage == i) {
                    <text> class="active"</text>
                } >
                @Html.ActionLink(i.ToString(), "ListNotes", "Patient", 
                                 new {ViewBag.patientId, Page = i.ToString() }, new {@Class="update-notes-list"})
            </li>
        }
        <li class="last">
            @Html.ActionLink("Last", "ListNotes", "Patient", 
                             new { ViewBag.patientId, Page = ViewBag.PageCount }, new {@Class="update-notes-list "} )
        </li>
    </ul>
</div>        
}

不优雅,但是当截止日期临近时,一个可行的解决方案比没有解决方案更好!