我正在尝试开发一个小项目。我有一个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>
几乎没有麻烦:
谢谢!