.net mysql操作_.Net数据库操作

e431349ce0d8c365ba8b3133a14b0971.png

8c30dd95643886cff3c5afbdaa04d40f.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 }

);

}

}

}

ae0ba8f030ba5ab6598b53ed9be9e326.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 }))

666b6d9a45e7f22cb1e774da6e9ecc45.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 }

);

}

}

}

3e18b394f2a4c778e785a14e8d965aeb.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"

})

}

05a545478314bab140b5c87333307c88.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" : "") // 增加高亮

})

}

c86204c57a62d560d6e1120c78d27288.png

此博文不清晰,作者都是晕头鸭。请搜索其他文章学习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值