在ASP.NET MVC中实现分页,排序和过滤功能

时间:2016-03-15 22:09:23

标签: c# asp.net-mvc sorting pagination filtering

我正在尝试开发一个小项目。我有一个View,我需要在其上实现分页,搜索和排序功能。我的模特是:

public class LicensesListViewModel
{
    public IEnumerable<License> Licenses { get; set; }
    public PageInfo PageInfo { get; set; }
    public SearchModel SearchModel { get; set; }
    public List<SortInfo> SortInfoList { get; set; }
}

PageInfo:

public class PageInfo
{
    public int CurrentPage { get; set; }
    public int ItemsPerPage { get; set; }
    public int TotalItems { get; set; }

    public int TotalPages
    {
        get { return (int) Math.Ceiling((double) TotalItems / ItemsPerPage); }
    }
}

SearchModel:

public class SearchModel
{
    public string Other { get; set; }
    public DateTime? DtFrom { get; set; }
    public DateTime? DtTo { get; set; }

    public string SelectedDep { get; set; }
    public SelectList DropDownDepartment { get; set; }

    public string SelectedIts { get; set; }
    public SelectList DropDownIts { get; set; }
}

和SortInfo:

public class SortInfo
{
    public string SortColumn { get; set; }
    public string SortOrder { get; set; }
}

因此,分页效果很好,但是当我尝试实现搜索和排序时,它会发生变化。这是我的控制器列表操作方法:

        public ViewResult List(SearchModel search, List<SortInfo> sortInfo, int page = 1)
    {
        if (search != null)
        {
            SearchModel sm = Session["Search"] as SearchModel;
            if (sm != null) // needed because dropdowns set to null?
            {
                search.DropDownDepartment = sm.DropDownDepartment;
                search.DropDownIts = sm.DropDownIts;
                Session["Search"] = search;
            }
            else Session["Search"] = search;
        }
        else search = Session["Search"] as SearchModel;

        LicensesListViewModel model = new LicensesListViewModel();
        model.Licenses = Repository.Licenses.Items;

        /*Get search condition*/
        Func<License, bool> predicate = GetFilter(search);
        if (predicate != null)
        {
            model.Licenses = model.Licenses.Where(predicate);
        }
        model.SearchModel = GetSearchModel(search);

        /*Get sorting condition*/
        if (sortInfo == null)
        {
            sortInfo = new List<SortInfo> {new SortInfo {SortColumn = "Id", SortOrder = "DESC"}};
        }
        else if (sortInfo.Count < 1)
        {
            sortInfo.Add(new SortInfo {SortColumn = "Id", SortOrder = "DESC"});
        }
        model.SortInfoList = sortInfo;
        foreach (SortInfo si in sortInfo)
        {
            switch(si.SortColumn)
            {
                case "Id":
                    model.Licenses = GetOrderBy(si.SortOrder, model.Licenses, license => license.Id);
                    break;
                case "CodeLic":
                    model.Licenses = GetOrderBy(si.SortOrder, model.Licenses, license => license.CodeLic);
                    break;
                case "CodeAct":
                    model.Licenses = GetOrderBy(si.SortOrder, model.Licenses, license => license.CodeAct);
                    break;
                case "ItsId":
                    model.Licenses = GetOrderBy(si.SortOrder, model.Licenses, license => license.ItsId);
                    break;
                case "Pib":
                    model.Licenses = GetOrderBy(si.SortOrder, model.Licenses, license => license.Pib);
                    break;
                case "DepartmentId":
                    model.Licenses = GetOrderBy(si.SortOrder, model.Licenses, license => license.DepartmentId);
                    break;
            }
        }

        model.Licenses = model.Licenses
                              .Skip((page - 1) * PageSize)
                              .Take(PageSize);

        /*Get paging*/
        model.PageInfo = new PageInfo
            {
                CurrentPage = page,
                ItemsPerPage = PageSize,
                TotalItems = predicate == null
                                 ? Repository.Licenses.Items.Count()
                                 : Repository.Licenses.Items.Where(predicate).Count()
            };

        return View(model);
    }

SearchModel显示在与列表相同的页面上的模态窗口的局部视图中:

@model LicenseManager.WebUI.Models.LicensesListViewModel
@{
    Html.RenderPartial("SearchView", Model.SearchModel);
} 
<div class="table-responsive">
</div>
<!-- Page numeration -->
@if (Model.PageInfo.TotalPages > 1)
{
<div class="text-center">
        <ul class="pagination pagination-sm">
            @Html.PageLinks(Model.PageInfo, i => Url.Action("List", new { page = i, search = Model.SearchModel}))
        </ul>
    </div>
}
<!-- Rows count -->
<div class="text-right">
    <span class="label label-default">Rows: @Model.PageInfo.TotalItems</span>
</div>

几乎没有麻烦:

  1. 当我尝试第一次搜索提交时,它工作正常,但是当我转到下一页时,搜索参数设置为null。 ViewBag也设置为null我已经尝试过了。如你所见,我决定使用Session [&#34; Search&#34;],但我不认为这是一个很好的决定。你能帮助我采取更好的变体吗?或者我如何使用AJAX执行此操作?这是一个不错的选择吗?
  2. 第二个问题是如何将List传递给List?我可以在哪里填写列名和排序顺序。或者至少只有一个列排序功能?
  3. 如何配置我的路由?
  4. 谢谢!

0 个答案:

没有答案