搜索并排序返回默认视图

时间:2015-04-22 11:08:06

标签: c# asp.net-mvc html5 entity-framework

我希望按照此处Asp.net/MVC Tutorial的方法设置我的用户数据视图,其中包含分页,排序和搜索功能。页面运行正常,调试数据显示过滤器和搜索正在被触发,但我一直将默认页面作为返回而不是过滤或排序列表。

索引

         @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 个答案:

答案 0 :(得分:2)

如果您继续返回没有过滤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());