本文共 12311 字,大约阅读时间需要 41 分钟。
Web.config配置数据库连接,
数据库相关类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.Entity; // 数据库支持using SportsStore.Domain.Entities;namespace SportsStore.Domain.Concrete{ public class EFDbContext : DbContext { public DbSetProducts { get; set; } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using SportsStore.Domain.Abstract;using SportsStore.Domain.Entities;namespace SportsStore.Domain.Concrete{ public class EFProductRepository : IProductRepository { private EFDbContext context = new EFDbContext(); public IEnumerableProducts { get { return context.Products; } } }}
莫名其妙就能得到数据了,
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using SportsStore.Domain.Abstract;using SportsStore.Domain.Entities;namespace SportsStore.WebUI.Controllers{ public class ProductController : Controller { private IProductRepository repository; public ProductController(IProductRepository productRepository) { this.repository = productRepository; } public ViewResult List() { return View(repository.Products); } }}
增加分页
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using SportsStore.Domain.Abstract;using SportsStore.Domain.Entities;namespace SportsStore.WebUI.Controllers{ public class ProductController : Controller { private IProductRepository repository; public int PageSize = 4; public ProductController(IProductRepository productRepository) { this.repository = productRepository; } public ViewResult List(int page = 1) { return View(repository.Products .OrderBy(p => p.ProductID) .Skip((page - 1) * PageSize) .Take(PageSize)); // 分页支持 } }}
分页模型
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace SportsStore.WebUI.Models{ public class PagingInfo { public int TotalItems { get; set; } public int ItemsPerPage { get; set; } public int CurrentPage { get; set; } public int TotalPages { get { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); } } }}
具体的分页模型
using System.Collections.Generic;using SportsStore.Domain.Entities;using System;using System.Linq;using System.Web;namespace SportsStore.WebUI.Models{ public class ProductsListViewModel { public IEnumerableProducts { get; set; } public PagingInfo PagingInfo { get; set; } }}
获取分页与数据
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using SportsStore.Domain.Abstract;using SportsStore.Domain.Entities;using SportsStore.WebUI.Models;using System.Web.Helpers;namespace SportsStore.WebUI.Controllers{ public class ProductController : Controller { private IProductRepository repository; public int PageSize = 4; public ProductController(IProductRepository productRepository) { this.repository = productRepository; } public ViewResult List(int page = 1) { ProductsListViewModel model = new ProductsListViewModel { Products = repository.Products .OrderBy(p => p.ProductID) .Skip((page - 1) * PageSize) .Take(PageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = repository.Products.Count() } }; return View(model); } }}
视图层,展示分页
@model SportsStore.WebUI.Models.ProductsListViewModel@{ ViewBag.Title = "Products";}@foreach (var p in Model.Products){}@p.Name
@p.Description@p.Price.ToString("c")
@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new { page = x }))
增加路由控制
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Web.Routing;namespace SportsStore.WebUI{ public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: null, url: "Page{page}", defaults: new { Controller = "Product", action = "List" } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Product", action = "List", id = UrlParameter.Optional } ); } }}
增加页面布局效果bootstrap
基础页面@ViewBag.Title Put something useful here later@RenderBody()
列表页面
@model SportsStore.WebUI.Models.ProductsListViewModel@{ ViewBag.Title = "Products";}@foreach (var p in Model.Products){}@p.Name @p.Price.ToString("c")
@p.Description@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new { page = x }))
ProductSummary
@model SportsStore.Domain.Entities.Product@Model.Name @Model.Price.ToString("c")
@Model.Description
改造List页面支持
@model SportsStore.WebUI.Models.ProductsListViewModel@{ ViewBag.Title = "Products";}@foreach (var p in Model.Products){ @Html.Partial("ProductSummary", p)}@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new { page = x }))
分页模型增加元素当前页
using System.Collections.Generic;using SportsStore.Domain.Entities;using System;using System.Linq;using System.Web;namespace SportsStore.WebUI.Models{ public class ProductsListViewModel { public IEnumerableProducts { get; set; } public PagingInfo PagingInfo { get; set; } public string CurrentCategory { get; set; } }}
控制器增加过滤改造
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using SportsStore.Domain.Abstract;using SportsStore.Domain.Entities;using SportsStore.WebUI.Models;using System.Web.Helpers;namespace SportsStore.WebUI.Controllers{ public class ProductController : Controller { private IProductRepository repository; public int PageSize = 2; public ProductController(IProductRepository productRepository) { this.repository = productRepository; } public ViewResult List(string category,int page = 1) { ProductsListViewModel model = new ProductsListViewModel { Products = repository.Products .Where(p => category == null || p.Category == category) .OrderBy(p => p.ProductID) .Skip((page - 1) * PageSize) .Take(PageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = repository.Products.Where(p => category == null || p.Category == category).Count() }, CurrentCategory = category }; return View(model); } }}
视图层,分页改造
@model SportsStore.WebUI.Models.ProductsListViewModel@{ ViewBag.Title = "Products";}@foreach (var p in Model.Products){ @Html.Partial("ProductSummary", p)}@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new { page = x, category = Model.CurrentCategory }))
路由处理
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Web.Routing;namespace SportsStore.WebUI{ public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(null, "", new { controller = "Product", action = "List", category = (string)null, page = 1 } ); routes.MapRoute(null, "Page{page}", new { controller = "Product", action = "List", category = (string)null }, new { page = @"\d+" } ); routes.MapRoute(null, "{category}", new { controller = "Product", action = "List", page = 1 } ); routes.MapRoute(null, "{category}/Page{page}", new { controller = "Product", action = "List" }, new { page = @"\d+" } ); routes.MapRoute(null, "{controller}/{action}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Product", action = "List", id = UrlParameter.Optional } ); } }}
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace SportsStore.WebUI.Controllers{ public class NavController : Controller { public string Menu() { return "Hello from NavController"; } }}
基础模板引入导航
@ViewBag.Title @Html.Action("Menu", "Nav")@RenderBody()
控制器获取分类数据
using System.Collections.Generic;using System.Web.Mvc;using SportsStore.Domain.Abstract;using System.Linq;namespace SportsStore.WebUI.Controllers{ public class NavController : Controller { private IProductRepository repository; public NavController(IProductRepository repo) { repository = repo; } public PartialViewResult Menu() { IEnumerablecategories = repository.Products .Select(x => x.Category) .Distinct() .OrderBy(x => x); return PartialView(categories); } }}
新建View页面
@model IEnumerable@Html.ActionLink("Home", "List", "Product", null, new { @class = "btn btn-block btn-default btn-lg" })@foreach (var link in Model){ @Html.RouteLink(link, new{ controller = "Product", action = "List", category = link, page = 1}, new{ @class = "btn btn-block btn-default btn-lg"})}
using System.Collections.Generic;using System.Web.Mvc;using SportsStore.Domain.Abstract;using System.Linq;namespace SportsStore.WebUI.Controllers{ public class NavController : Controller { private IProductRepository repository; public NavController(IProductRepository repo) { repository = repo; } public PartialViewResult Menu(string category = null) { ViewBag.SelectedCategory = category; IEnumerablecategories = repository.Products .Select(x => x.Category) .Distinct() .OrderBy(x => x); return PartialView(categories); } }}
@model IEnumerable@Html.ActionLink("Home", "List", "Product", null, new { @class = "btn btn-block btn-default btn-lg" })@foreach (var link in Model){ @Html.RouteLink(link, new{ controller = "Product", action = "List", category = link, page = 1}, new{ @class = "btn btn-block btn-default btn-lg" + (link == ViewBag.SelectedCategory ? " btn-primary" : "") // 增加高亮})}
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6962120.html,如需转载请自行联系原作者