以下是一个使用ASP.NET MVC实现分页列表、宽松复选框列表过滤器的示例代码:
FilterViewModel
的模型类,用于存储过滤器的值:public class FilterViewModel
{
public string SearchString { get; set; }
public List SelectedOptions { get; set; }
}
List
类型的选项列表,并将其传递给视图:public IActionResult Index()
{
List options = new List
{
"Option 1",
"Option 2",
"Option 3",
// 添加其他选项
};
ViewBag.Options = options;
return View();
}
@model FilterViewModel
@using (Html.BeginForm("Index", "YourControllerName", FormMethod.Get))
{
@Html.Label("Search:")
@Html.TextBoxFor(m => m.SearchString)
@foreach (string option in ViewBag.Options)
{
}
Column 1
Column 2
@foreach (var item in Model.Items)
{
@item.Column1
@item.Column2
}
@Html.PagedListPager(Model.PagedList, page => Url.Action("Index", new { page }))
}
Index
动作中,根据过滤器的值进行查询并返回分页列表的视图:public IActionResult Index(FilterViewModel filter, int? page)
{
var items = // 根据过滤器的值查询数据
int pageSize = 10;
int pageNumber = page ?? 1;
var pagedList = items.ToPagedList(pageNumber, pageSize);
var model = new FilterViewModel
{
SearchString = filter.SearchString,
SelectedOptions = filter.SelectedOptions,
Items = pagedList
};
return View(model);
}
请注意,上述示例中使用了ToPagedList
方法来实现分页功能,您需要在项目中安装适当的分页库(如PagedList.Mvc)并引用相关命名空间。