嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
asp.net mvc5 一个关于 分页标签的实例,非常实用
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ShowPin.MvcPaging.Demo.Models;
namespace ShowPin.MvcPaging.Demo.Controllers
{
public class PagingController : Controller
{
int _currentPage = 1;
int _pageSize = 10;
private readonly string[] _allCategories = { "吃的", "穿的", "用的" };
private readonly IList<Product> _allProducts = new List<Product>();
public PagingController()
{
InitializeProducts();
}
//简单初始化产品集合
private void InitializeProducts()
{
for (var i = 0; i < 122; i )
{
var product = new Product
{
Name = "产品 " (i 1)
};
var categoryIndex = i % 4;
if (categoryIndex > 2)
{
categoryIndex = categoryIndex - 3;
}
product.Category = _allCategories[categoryIndex];
_allProducts.Add(product);
}
}
//简单的分页
public ActionResult Index(int? page = 1, int? size = 10)
{
_currentPage = page.HasValue ? page.Value : _currentPage;
_pageSize = size.HasValue ? size.Value : _pageSize;
var model = _allProducts.ToPagedList(_currentPage, _pageSize);
return View(model);
}
//根据产品分类搜索分页
public ActionResult PagingByCategory(string categoryName, int? page = 1, int? size = 10)
{
_currentPage = page.HasValue ? page.Value : _currentPage;
_pageSize = size.HasValue ? size.Value : _pageSize;
categoryName = categoryName ?? _allCategories[0];
var model = _allProducts.Where(m => m.Category == categoryName)
.ToPagedList(_currentPage, _pageSize);
ViewBag.CategoryName = new SelectList(_allCategories, categoryName);
ViewBag.CategoryDisplayName = categoryName;
return View(model);
}
//根据多个产品分类搜索分页
public ActionResult PagingByCategories(string[] categories, int? page = 1, int? size = 10)
{
_currentPage = page.HasValue ? page.Value : _currentPage;
_pageSize = size.HasValue ? size.Value : _pageSize;
categories = categories ?? new string[0];
var model = _allProducts.Where(m => categories.Contains(m.Category))
.ToPagedList(_currentPage, _pageSize);
ViewBag.AllCategories = _allCategories;
ViewBag.SelectedCategories = categories;
return View(model);
}
//使用路由名称分页
public ActionResult PagingByRoute()
{
return View();
}
//使用路由名称分页,为了模拟这种特殊的需求,所以使用了特殊的方式
public ActionResult ShowPagingByRoute(int? page = 1, int? size = 10)
{
_currentPage = page.HasValue ? page.Value : _currentPage;
_pageSize = size.HasValue ? size.Value : _pageSize;
var model = _allProducts.ToPagedList(_currentPage, _pageSize);
return PartialView("_PartialPagingByRoute", model);
}
//ajax分页
public ActionResult Ajax()
{
return View();
}
//ajax分页
public ActionResult AjaxPage(int? page = 1, int? size = 10)
{
_currentPage = page.HasValue ? page.Value : _currentPage;
_pageSize = size.HasValue ? size.Value : _pageSize;
var model = _allProducts.ToPagedList(_currentPage, _pageSize);
return PartialView("_ProductGrid", model);
}
//自定义模版
public ActionResult Bootstrap(int? page = 1, int? size = 10)
{
_currentPage = page.HasValue ? page.Value : _currentPage;
_pageSize = size.HasValue ? size.Value : _pageSize;
var model = _allProducts.ToPagedList(_currentPage, _pageSize);
return View(model);
}
//自定义页码路由键值对
public ActionResult CustomPageRouteValueKey(SearchModel search)
{
if (search.Page != null) _currentPage = (int)search.Page;
var model = _allProducts.ToPagedList(_currentPage, _pageSize);
return View(model);
}
}
}