0

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

@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 调用。我可能错过了什么?

4

1 回答 1

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);
    }

剃刀视图:

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

不优雅,但是当最后期限临近时,一个可行的解决方案总比没有解决方案好!

于 2013-10-21T10:01:47.643 回答