1

我在搜索操作结果中使用分页列表来分页我的结果。它返回日期范围或类别内的事件。返回的第一页很好,但第二页没有任何结果。当我检查断点时,查询返回超过 7 个结果。这是我的搜索控制器;

   public ActionResult Search(DateTime? q, DateTime? e, int? EventCategoryId, Event model)
    {
        var SearchEvents = db.Events
            .OrderByDescending(r => r.start)
            .Where(r => 
                r.start >= q && 
                r.end <= e && 
                r.EventCategoryId == EventCategoryId || 
                r.start >= q || 
                r.EventCategoryId == EventCategoryId);

        ViewBag.QValue = model.start;

        ViewBag.EValue = model.end;

        ViewBag.ListValue = model.EventCategoryId;

        ViewBag.EventCategoryId = new SelectList(db.EventsCategories, "Id", "Name", ViewBag.ListValue);

        var pageIndex = model.Page ?? 1;
        var results = SearchEvents.ToPagedList(pageIndex, 4);
        ViewBag.Names = results;

        return View(results);
        }
    }

这是我的观点;

@model IPagedList<RealKaac.Models.Event>

@{
    ViewBag.Title = "SearchResults";
}

@using PagedList;
@using PagedList.Mvc;
@using System.Linq;

<div id="Content">
    <h2>SearchResults</h2>
    <div id="EventSearch">
    @using (Html.BeginForm("Search", "Event"))
    {
        <p class="EventPar">Start Date: </p>
        <input id ="datepicker" type ="text" name ="q" value="@ViewBag.QValue" />
            <p class="EventPar">End Date: </p>
        <input id ="datepickerend" type ="text" name ="e" value ="@ViewBag.EValue"  />
        <p class="EventPar">Category:</p>
        @Html.DropDownList("EventCategoryId")
        <input id="EventButton" style="padding:1px;" type ="submit" name ="Search" value = "Search" />
    }
    </div>

    <div id="IndexEvents">
    @foreach (var item in Model)
    {
        <div class ="event">   
            <div class="eventname">
                @*<p>@Html.DisplayFor(modelItem => item.Name)</p>    *@
            </div>
            <div class = "etitle">
                 <p>@Html.DisplayFor(modelItem => item.title)</p>
            </div>
            <div class="eventsdesc">
                <p>@Html.DisplayFor(modelItem => item.EventDescription)</p>
            </div>
            <ul class ="datelink">
                <li style="margin-right:120px;">This event starts on the
                    @Html.DisplayFor(modelItem => item.start) and ends on the
                    @Html.DisplayFor(modelItem => item.end)</li>
                <li>Click @Html.DisplayFor(modelItem => item.EventWebsite) for more info.</li>
            </ul>
        </div>
    }
    </div>
</div>

@Html.PagedListPager((IPagedList)ViewBag.Names, page => Url.Action("Search", new { page }))

我对此很陌生,所以我可能遗漏了一些小东西,非常感谢任何建议。

谢谢。

4

1 回答 1

2

Url.Action 只有一个参数(页面#),它与控制器的方法不匹配。它不提供对任何其他参数(例如开始/结束日期)的访问。因此,接下来 4 个事件的第二个“页面”查询可能不起作用。如果您创建了一个具有必要属性的 SearchModel,您应该能够轻松地将值从一页发送到下一页。

public ActionResult Search(SearchModel search) { 
    //... (your search code)
    // ... then, before returning the View,
    ViewBag.SearchParameters = search;
    return View(results);

其中 SearchModel 具有如下属性:

public class SearchModel {
   public DateTime? StartDate { get; set; }
   public DateTime? EndDate { get; set; }
   public int Page? { get; set; }
   // etc.
}

然后,您可以:

 @Html.PagedListPager((IPagedList)ViewBag.Names, page => Url.Action("Search", new SearchModel { search = ViewBag.SearchParameters }))

或者你可以只使用命名参数:

@Html.PagedListPager((IPagedList)ViewBag.Names, page => Url.Action("Search", new { page = page, q = ViewBag.QValue, /* ... etc. */ }))
于 2011-11-28T21:45:24.967 回答