zoukankan      html  css  js  c++  java
  • MVC 中 Razor 无限分类的展示

    在MVC的Razor视图展示无级分类的办法,在网上看了很多资料,大多搞得很高大上。可能本人水平有限,实在是不会用。

    那我就用最简单爆力的办法来做。

    Model:

        public class NewsCategory
        {
            [Key]
            public int CategoryId { get; set; }
            public int ParentCategoryId { get; set; }
            [Required]
            [StringLength(50)]
            public string CategoryName { get; set; }
        }

    ViewModel

        public class NewsCategoriesViewModel
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public List<NewsCategoriesViewModel> children { get; set; }
        }

    Controller

    递归获取数据,然后返回给视图

     1        abcContext db = newabcContext();
     2         public ActionResult Index()
     3         {
     4             var categoryList = GetCategoryList(0);
     5             return View(categoryList);
     6         }
     7 
     8         [NonAction]
     9         public List<NewsCategoriesViewModel> GetCategoryList(int Id)
    10         {
    11             List<NewsCategoriesViewModel> uvModel = new List<NewsCategoriesViewModel>();
    12 
    13 
    14             var perentList = db.Set<NewsCategory>().Where(p => p.ParentCategoryId == Id).ToList();
    15             
    16             if (perentList.Count > 0)
    17             {
    18                 foreach (var item in perentList)
    19                 {
    20                     NewsCategoriesViewModel userViewModel = new NewsCategoriesViewModel
    21                     {
    22                         Id = item.CategoryId,
    23                         Name = item.CategoryName,
    24                         children = new List<NewsCategoriesViewModel>()
    25                     };
    26                     List<NewsCategoriesViewModel> tempList = GetCategoryList(item.CategoryId);
    27                     if (tempList.Count > 0)
    28                     {
    29                         //这里出错了;
    30                         //userViewModel.children.Add(tempList);
    31                         userViewModel.children = tempList;
    32                     }
    33                     uvModel.Add(userViewModel);
    34                 }
    35             }
    36             return uvModel;
    37         }
    38     }

    View

    定义一个视图方法,然后递归调用。

    @model List<NewsCategoriesViewModel>
    @helper DisplayList(List<NewsCategoriesViewModel> model)
            {
                if (model.Count > 0)
                {
                    <ul>
                        @foreach (var item in model)
                        {
                            <li>@item.Name</li>
                            if (item.children.Count > 0)
                            {
                                @DisplayList(item.children);
                            }
                        }
                    </ul>
    
                }
            }
            @DisplayList(Model)

    打完收功!

  • 相关阅读:
    Oracle查询今天的数据(昨天、本周...)
    Windows添加删除 route
    大三寒假学习进度(九)
    大三寒假学习进度(八)
    大三寒假学习进度(七)
    大三寒假学习进度(六)
    大三寒假学习进度(五)
    《软件架构师应该知道的97件事》阅读笔记(一)
    大三寒假学习进度(四)
    大三寒假学习进度(三)
  • 原文地址:https://www.cnblogs.com/micenote/p/5032497.html
Copyright © 2011-2022 走看看