坚持使用类别的多级菜单 [英] Stuck on multilevel menu with categories

查看:53
本文介绍了坚持使用类别的多级菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我关注此链接在Asp.Net MVC中循环通过多级动态菜单



它真的很有意义,但现在我是坚持这个。当我在LINQ querry中有一个方法时,我犯了一个错误。它无法将IEnumerable转换为通用列表?我该如何解决这个问题。尝试在方法之后设置.ToList()。



我已经从我的类别模型制作了我的Viewmodel



Hi
I am following this link Loop Through Multi-Level Dynamic Menus in Asp.Net MVC

It makes really sence, but now I am stuck on this. I makes an error when I have a method inside the LINQ querry. It cannot convert IEnumerable to an genericlist? How do I solve this problem. Have try to set .ToList() after the method.

I have made my Viewmodel from my categories model

public class Category
    {
        public int CategoryId { get; set; }

        [Required(ErrorMessage = "Fill in a Name")]
        public string Name { get; set; }

        public int? ParentCategoryId { get; set; }
        public Category ParentCategory { get; set; }

        public virtual ICollection Listings { get; set; }
    }



到此Viewmodel:




To this Viewmodel :

public class MenuViewmodel
    {
        public int MenuId { get; set; }
        public string Name { get; set; }

        public List Children { get; set; }
    }

On my controller

<pre lang="c#">
[ChildActionOnly]
        public ActionResult Menus()
        {
            
            List menusource = categoriesRepository.AllIncluding().ToList(); // Get menus from here
           var model = CreateVM(0, menusource); //Transform it into the viewmodel
            return PartialView("_Menu");
        }
        public IEnumerable CreateVM(int parentId, List source)
        {
            
            var model = (from p in categoriesRepository.AllIncluding() select p).ToList();
            model = from m in model where m.CategoryId == parentId select new
                  MenuViewmodel() {
                MenuId = m.CategoryId,
                Name = m.Name,
                Children = CreateVM(m.CategoryId, source)

            };

            return model;
        }

推荐答案

我将稍微重新排序代码,以便您更清楚地看到关系:



I am going to re-order the code a bit so you can see relationship more clearly:

public ActionResult Menus(){
    List<Menu> menusource; // get your menus here
    ViewBag.Menus = CreateVM(0, menusource);  // transform it into the ViewModel
    return View();
}





上面的块创建一个空列表并传递给CreateVM,CreateVM返回一个填充的列表,该列表被分配给使用Viewbag的视图





The block above is creating an empty list and passing to CreateVM, CreateVM returns a populated list which is assigned to the the View using the Viewbag

@{ 
    var menusList = ViewBag.Menus as IEnumerable<MenuViewModel>; 
    Html.RenderPartial("MenuPartial", menuslist);
}





menulist作为ViewBag.Menus传递,并作为菜单项列表传递到名为的局部视图中MenuPartial



部分视图如下:





The menulist is passed as ViewBag.Menus and is passed as a list of menu items into the partial view called "MenuPartial"

The partial view looks like this:

@model IEnumerable<MenuViewModel>

@foreach (var menuitem in model)
{
    <ul>
        <li>
            <h1>@menuitem.Name</h1>
            @{
                Html.RenderPartial("MenuPartial", menuitem.Children);
            }
        </li>
    </ul>
}





渲染视图时填充@model -



The @model is populated when rendering the view -

Html.RenderPartial("MenuPartial", menuslist);





对于每个菜单项,它将显示当前列表项(menueitem) .Name)然后项目子列表(深入到层次结构中)及其子项及其子项等。



要解决的最后一部分是CreateVM函数。





For each menu item it will display the current list items (menueitem.Name) and then the items children list (going deeper into the hierarchy) and their children and their children etc.

The last part to address is the CreateVM function.

public IEnumerable<MenuViewModel> CreateVM(int parentid, List<Menu> source)
{
    return from men in source
           where men.ParentId = parentid
           select new MenuViewModel(){
                      MenuId = men.MenuId, 
                      Name = men.Name
                      // other properties
                      Children = CreateVM(men.MenuId, source)
                  };
}





此例程还递归地构建要显示的列表。我强调了函数调用自身的位置。因此,以相同的方式构建列表以最终传递回调用例程。



我认为可能缺少的是如何加载数据来创建层次结构。以下是伪代码,但应该是说明性的:





This routine is also recursively building a list to be displayed. I have underlined where the function calls itself. So in the same way it is building up the list to eventually pass back to the calling routine.

I think what might be missing is how the data is loaded to create the hierarchy. The following is psuedo code but should be illustrative:

List<menuviewmodel> mm = new List<menuviewmodel>();
mm.add(new MenuViewmodel() {id=1, name="one"});
//now there is one item at the root level
mm.add(new MenuViewmodel() {id=2, name="two"});
//now there are two at first level.
</menuviewmodel></menuviewmodel>





让我们为第一项添加一个子项目





Lets add a subitem to the first item

mm[0].Children = new List<menuviewmodel>();
mm[0].Children.add(new MenuViewmodel() {id=10, name="One - One"});
</menuviewmodel>





现在你有一个MenuViewmodel列表,它还包含MenuViewmodel第一项下的另一个列表。



希望这会有所帮助。



Now you have a list of MenuViewmodel which also contains another list under the first item of MenuViewmodel.

Hope this helps.


这篇关于坚持使用类别的多级菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆