【发布时间】:2019-07-28 18:34:39
【问题描述】:
我正在尝试让分页与多个搜索和排序顺序一起使用。当我进行搜索时,返回的第一页是结果集的正确页面,但是当我使用分页按钮选择下一页时,我的搜索不再考虑,我得到了所有结果。
我的控制器如下所示:
public ActionResult Index(string sortOrder, string searchString, string currentFilter, int? page, string Site, string Name, string Acct, string City, string State, string Address, string Status, string Type)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.AddressSortParm = sortOrder == "Address" ? "addr_desc" : "Address";
ViewBag.SiteSortParm = sortOrder == "Site" ? "site_desc" : "Site";
ViewBag.CitySortParm = sortOrder == "City" ? "city_desc" : "City";
ViewBag.StateSortParm = sortOrder == "State" ? "state_desc" : "State";
if (searchString != null || Site != null || Name != null || Acct != null || City != null || State != null || Address != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var geocodes = from s in db.CFN_Geocodes select s;
if (!String.IsNullOrEmpty(Name))
{
geocodes = geocodes.Where(s => s.NAME.Contains(Name));
}
if (!String.IsNullOrEmpty(Site))
{
geocodes = geocodes.Where(s => s.CFN_SITE.Contains(Site));
}
if (!String.IsNullOrEmpty(Address))
{
geocodes = geocodes.Where(s => s.STREET1.Contains(Address));
}
if (!String.IsNullOrEmpty(City))
{
geocodes = geocodes.Where(s => s.CITY.Contains(City));
}
if (!String.IsNullOrEmpty(State))
{
geocodes = geocodes.Where(s => s.STATE_CODE.Contains(State));
}
if (!String.IsNullOrEmpty(Acct))
{
geocodes = geocodes.Where(s => s.AccountNumber.Contains(Acct));
}
if (!String.IsNullOrEmpty(Status))
{
geocodes = geocodes.Where(s => s.Status.ToString().Contains(Status));
}
if (!String.IsNullOrEmpty(Type))
{
geocodes = geocodes.Where(s => s.SiteType.Contains(Type));
}
switch (sortOrder)
{
case "name_desc":
geocodes = geocodes.OrderByDescending(s => s.NAME);
break;
case "Address":
geocodes = geocodes.OrderBy(s => s.STREET1);
break;
case "addr_desc":
geocodes = geocodes.OrderByDescending(s => s.STREET1);
break;
case "Site":
geocodes = geocodes.OrderBy(s => s.CFN_SITE);
break;
case "site_desc":
geocodes = geocodes.OrderByDescending(s => s.CFN_SITE);
break;
case "City":
geocodes = geocodes.OrderBy(s => s.CITY);
break;
case "city_desc":
geocodes = geocodes.OrderByDescending(s => s.CITY);
break;
case "State":
geocodes = geocodes.OrderBy(s => s.STATE_CODE);
break;
case "state_desc":
geocodes = geocodes.OrderByDescending(s => s.STATE_CODE);
break;
default:
geocodes = geocodes.OrderBy(s => s.NAME);
break;
}
int pageSize = 20;
int pageNumber = (page ?? 1);
return View(geocodes.ToPagedList(pageNumber, pageSize));
}
我的观点是这样的:
@model PagedList.IPagedList<Intranet.Models.CardlockSiteMgr.CFN_Geocodes>
@using PagedList.Mvc
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "Site Manager";
}
<h2>Site Manager</h2>
<h5>@Html.ActionLink("Add New Site", "Create")</h5>
@using (Html.BeginForm())
{
<table class="table table-responsive table-condensed">
<tr>
<td colspan="6"><h4>Search Sites</h4></td>
</tr>
<tr>
<td>
Site:
</td>
<td>
@Html.TextBox("Site")
</td>
<td>
Account:
</td>
<td>
@Html.TextBox("Acct")
</td>
<td>
Name:
</td>
<td>
@Html.TextBox("Name")
</td>
</tr>
<tr>
<td>
Address:
</td>
<td>
@Html.TextBox("Address")
</td>
<td>
City:
</td>
<td>
@Html.TextBox("City")
</td>
<td>
State:
</td>
<td>
@Html.TextBox("State")
</td>
</tr>
<tr>
<td>
Status:
</td>
<td>
<select name="Status">
<option></option>
<option>Active</option>
<option>Inactive</option>
</select>
</td>
<td>
Type:
</td>
<td>
<select name="Type">
<option></option>
<option>CFN</option>
<option>Fleetwide</option>
</select>
</td>
<td></td>
<td>
<input type="submit" value="Search" />
</td>
</tr>
</table>
}
<table class="table table-responsive table-striped">
<tr>
<th>
@Html.ActionLink("Site", "Index", new { sortOrder = ViewBag.SiteSortParm })
</th>
<th>
@Html.ActionLink("Name", "Index", new { sortOrder = ViewBag.NameSortParm })
</th>
<th>
@Html.ActionLink("Address", "Index", new { sortOrder = ViewBag.AddressSortParm })
</th>
<th>
@Html.ActionLink("City", "Index", new { sortOrder = ViewBag.CitySortParm })
</th>
<th>
@Html.ActionLink("State", "Index", new { sortOrder = ViewBag.StateSortParm })
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CFN_SITE)
</td>
<td>
@Html.DisplayFor(modelItem => item.NAME)
</td>
<td>
@Html.DisplayFor(modelItem => item.STREET1)
</td>
<td>
@Html.DisplayFor(modelItem => item.CITY)
</td>
<td>
@Html.DisplayFor(modelItem => item.STATE_CODE)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.SITE_ID })
</td>
</tr>
}
</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
关于我在这里做错了什么有什么想法吗?
【问题讨论】:
-
如果您有... searchString 或 Site 或 Name 或 Acct 或 City 或 State 或 Address 您的页面将设置为 1 ....
-
是的,但即使我删除了该代码,我也会得到相同的行为。在我尝试点击分页栏中的下一个按钮后,它最终会向我显示所有结果,而不仅仅是我搜索的结果。
标签: c# html razor model-view-controller pagination