博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.Net数据库操作
阅读量:6961 次
发布时间:2019-06-27

本文共 12311 字,大约阅读时间需要 41 分钟。

422101-20170608115823903-663106264.png

422101-20170608115827872-2112140274.png

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 DbSet
Products { 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 IEnumerable
Products { 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 IEnumerable
Products { 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 }            );        }    }}

422101-20170608171649231-2065177549.png

增加页面布局效果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 }))

422101-20170608172622684-77449551.png

增加页面模板,可以多处使用

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 IEnumerable
Products { 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 }            );        }    }}

422101-20170608181134153-1590225365.png

增加导航控制器

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()        {            IEnumerable
categories = 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"})}

422101-20170608183106653-273177148.png

菜单增加高亮

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;            IEnumerable
categories = 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" : "") // 增加高亮})}

422101-20170608183705981-2119641620.png

本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6962120.html,如需转载请自行联系原作者

你可能感兴趣的文章
MySQL单列索引和组合索引的区别介绍
查看>>
Git命令
查看>>
aws S3 util demo
查看>>
react native ios 网络请求问题
查看>>
python文件处理
查看>>
Oracle高级应用之物化视图(materialized view)
查看>>
自定义文件上传样式,该方法几乎可以覆盖其他一切浏览原生样式
查看>>
图片上传后即时预览
查看>>
Django滚动logger
查看>>
负载均衡笔记
查看>>
Maven的使用,Nexus建立本地仓库以及Eclipse导入Maven项目(三): 配置篇
查看>>
装系统
查看>>
如何准备BAT技术面试答案(上)——Java研发方向
查看>>
查找算法(1)--二分查找
查看>>
apache tomcat负载均衡总结
查看>>
深入理解Tomcat系列之二:源码调试环境搭建
查看>>
java简易聊天程序
查看>>
Redis-Cluster实战-
查看>>
Maven提高篇系列之四——使用Profile
查看>>
配置sonar、jenkins进行持续审查
查看>>