[asp.net-mvc]搜索和排序返回默认视图

发布时间: 2016/10/7 2:26:16
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我期待Asp.net/MVC 教程设置分页、 排序和搜索功能的办法,在这里我使用的数据视图。在页运行正确,与调试数据显示筛选器和搜索被触发,但是老是默认页作为回报而不是已筛选或排序的列表。

索引

         @using (Html.BeginForm("Index", "Users", FormMethod.Get))
            {
                <p>
                    Find by Name: @Html.TextBox("searchString", ViewBag.CurrentFilter as string)
                    <input type="submit" value="Search"/>
                </p>
            }

        <table class="table">
            <tr>
                <th>
                    Email
                </th>
                <th>
                    FirstName
                </th>
                <th>
                    @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortPram, currentFilter = ViewBag.CurrentFilter })
                </th>
                <th>
                    @Html.ActionLink("Create Date", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
                </th>
            </tr>

    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CreatedDate)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.UserId }) |
                @Html.ActionLink("Details", "Details", new { id=item.UserId }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.UserId })
            </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 }))

控制器

public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortPram = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "name_asc";
ViewBag.DateSortPram = sortOrder == "Date" ? "date_desc" : "Date";

if (searchString != null)
{
    page = 1;
}
else
{
    searchString = currentFilter;
}

ViewBag.CurrentFilter = searchString;

var users = from u in db.Users
    select u;

if (!String.IsNullOrEmpty(searchString))
{
    Debug.WriteLine("Searching for " + searchString);
    users =
        users.Where(
            u =>
                u.LastName.ToUpper().Contains(searchString.ToUpper()) ||
                u.FirstName.ToUpper().Contains(searchString.ToUpper())
            );
}
switch (sortOrder)
{
    case "name_desc":
        Debug.WriteLine("Changing order " + sortOrder);
        users = users.OrderByDescending(u => u.LastName);
        break;
    case "name_asc":
        Debug.WriteLine("Changing order " + sortOrder);
        users = users.OrderBy(u => u.LastName);
        break;
    case "Date":
        Debug.WriteLine("Changing order " + sortOrder);
        users = users.OrderByDescending(u => u.CreatedDate);
        break;
    default:
        Debug.WriteLine("Changing order " + sortOrder);
        users = users.OrderBy(u => u.UserId);
        break;
}

int pageSize = 3;
int pageNumber = (page ?? 1);

return View(db.Users.OrderBy(u => u.UserId).ToPagedList(pageNumber, pageSize));

}

解决方法 1:

如果你保持返回不带筛选的新查询,u 又得到已筛选或排序的结果 b.Users.OrderBy(u => u.UserId) :

返回错误︰

return View(db.Users.OrderBy(u => u.UserId).ToPagedList(pageNumber, pageSize));

你应该 users 对象进行筛选和排序。

更新 1︰ 尝试像︰

return View(users.Skip(pageNumber*pageSize).Take(pageSize).ToList());
赞助商