博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
主攻ASP.NET.4.5.1 MVC5.0之重生:根据产品类别显示菜单分类和分页
阅读量:4970 次
发布时间:2019-06-12

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

路径访问的几种方式和分页效果

显示其它类别的效果和多数据分页效果

默认访问网站路径效果和多数据分页效果

 


URL路径访问可页面

http://localhost:5339/stationery

http://localhost:5339/stationery/Page2

http://localhost:5339/?category=fashion

http://localhost:5339/?category=stationery

 

主要还是看代码和书,写出来的代码

主要使用这个路径显示和分页的效果

主要还是根据类别生成菜单显示的方法比较实用,请关注下面代码

关注路由配置

测试数据和数据库表

添加ProductsListViewModel.cs 编写如下代码

namespace Toad.WebUI.Models{    public class ProductsListViewModel    {        public IEnumerable
Products { get; set; } public PagingInfo PagingInfo { get; set; } public string CurrentCategory { get; set; } }}

 

 ProductController代码

namespace Toad.WebUI.Controllers{    public class ProductController : Controller    {        private IProductRepository repository;        public int PageSize = 4;        public ProductController(IProductRepository productRepository)        {            this.repository = productRepository;        }        public ViewResult List(string category, int page = 1)        {            ProductsListViewModel viewModel = 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 = category == null ?                        repository.Products.Count() :                        repository.Products.Where(e => e.Category == category).Count()                },                CurrentCategory = category            };            return View(viewModel);        }        ////        //// GET: /Product/        //public ActionResult List()        //{        //    return View(repository.Products);        //}    }}

 

路由配置代码

namespace Toad.WebUI{    public class RouteConfig    {        public static void RegisterRoutes(RouteCollection routes)        {            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            routes.MapRoute(null,                         "",                         new                         {                             controller = "Product",                             action = "List",                             category = UrlParameter.Optional,                             page = UrlParameter.Optional                         }                     );            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 }            //);        }    }}

HtmlHelpers/PagingHelpers.cs分页代码

namespace Toad.WebUI.HtmlHelpers{    public static class PagingHelpers    {        public static MvcHtmlString PageLinks(this HtmlHelper html,                                              PagingInfo pagingInfo,                                              Func
pageUrl) { StringBuilder result = new StringBuilder(); for (int i = 1; i <= pagingInfo.TotalPages; i++) { TagBuilder tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrl(i)); tag.InnerHtml = i.ToString(); if (i == pagingInfo.CurrentPage) { tag.AddCssClass("selected"); tag.AddCssClass("btn-primary"); } tag.AddCssClass("btn btn-default"); result.Append(tag.ToString()); } return MvcHtmlString.Create(result.ToString()); } }}

Models\PagingInfo.cs代码

namespace Toad.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); }        }    }}

NavController.cs代码

namespace Toad.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); } }}

视图部分Shared

Menu.cshtml

@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" : "")})}

 

 

Product视图部分

List.cshtml

@using Toad.WebUI.HtmlHelpers@model Toad.WebUI.Models.ProductsListViewModel@{    ViewBag.Title = "Products";    Layout = "~/Views/Shared/_Layout.cshtml";}@foreach (var p in Model.Products){    @Html.Partial("ProductSummary", p)}
@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new { page = x, category = Model.CurrentCategory }))

 

 

 

视图部分Shared

 

ProductSummary.cshtml

@model Toad.Domain.EFDB.Products

@Model.Name @Model.Price.ToString("c")

@using (Html.BeginForm("AddToCart", "Cart")) {
@Html.HiddenFor(x => x.ProductID) @Html.Hidden("returnUrl", Request.Url.PathAndQuery)
}
@Model.Description

 

 

_Layout.cshtml

    
@ViewBag.Title
@Html.Action("Menu", "Nav")
@RenderBody()

 

代码下载链接: http://pan.baidu.com/s/1eQgjZnO 密码: vdnr

声明:本博客高度重视知识产权保护,发现本博客发布的信息包含有侵犯其著作权的链接内容时,请联系我,我将第一时间做相应处理,联系邮箱。

 

作者:    来源:

说明:未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如有疑问,可以通过  联系作者,本文章采用 进行许可

 

 

转载于:https://www.cnblogs.com/cube/p/3716412.html

你可能感兴趣的文章
.net Tuple特性
查看>>
Java基础常见英语词汇
查看>>
nginx启动、关闭命令、重启nginx报错open() "/var/run/nginx/nginx.pid" failed
查看>>
BZOJ 3097 Hash Killer I
查看>>
UINavigationController的视图层理关系
查看>>
html阴影效果怎么做,css 内阴影怎么做
查看>>
宏观经济
查看>>
综合练习:词频统计
查看>>
BZOJ1026: [SCOI2009]windy数
查看>>
样板操作数
查看>>
64位UBUNTU下安装adobe reader后无法启动
查看>>
组件:slot插槽
查看>>
Nginx配置文件nginx.conf中文详解(转)
查看>>
POJ 1308 Is It A Tree?(并查集)
查看>>
N进制到M进制的转换问题
查看>>
利用sed把一行的文本文件改成每句一行
查看>>
Android应用开发:核心技术解析与最佳实践pdf
查看>>
python——爬虫
查看>>
孤荷凌寒自学python第五十八天成功使用python来连接上远端MongoDb数据库
查看>>
求一个字符串中最长回文子串的长度(承接上一个题目)
查看>>