简体   繁体   中英

Multi Array in C# MVC

How I create Data Array in C# MVC with format array:

MenuHeader
    MenuDetail, Attribute1, Attribute2
MenuHeader2
    MenuDetail, Attribute1, Attribute2
    MenuDetail, Attribute1, Attribute2
    MenuDetail, Attribute1, Attribute2

Please Help.

Thank you.

Usually menus aren't organized in array because they can be nested in each other so they have an hierarchy, one possible way is to create a class

class MenuItem 
{
    public MenuItem[] Childs { get; set; }
    public String Detail { get; set; }
    public String[] Attributes { get; set; }
}

I have never used MenuDetails and such and I have no idea about the right way to do it but what you can do is this:

Step 1

create a class which takes three properties

class MenuDetailsAndAttributes
{
    public MenuDetail menudetail ;
    public Attribute attribute1 ;
    public Attribute attribute2 ;

    public MenuDetailsAndAttributes(/*some parametrs  maybe*/)
    {
        menudetail = new MenuDetail(/*I dont know what to put here, you will need to work them out yourself*/);
        attribute1 = new Attribute();
        attribtue2 = new Attribute();
    }

}

Step 2

And After this make another class

class HeaderAndDetails
{
    MenuHeader header;
    public List<MenuDetailsAndAttributes> list;

    public HeaderAndDetails()
    {
        header = new MenuHeader();
        list = new List<MenuDetailsAndAttributes>();
    }
}

Step 4

Now you can write tons of unnecessary code to add/remove/set/change and construct every class inside these lists

Important Note

I hope you understand what I did there. What I tried to do ther is combine

MenuDetail, Attribute1, Attribute2

into a class, and combine a list of that class with MenuHeader and create a new class, and create list of that class so you can use it as many times you want.

I guess this is very wrong and unnecessarily long but you can pull it off.

thanks for respons.

Array
(
    [0] => Array
        (
            [MenuId] => 1
            [MenuName] => Menu Header
            [MenuAttribute] =>
            [MenuChild] => Array
                (
                    [0] => Array
                        (
                            [MenuId] => 6
                            [MenuName] => Sub Menu 1
                            [MenuIcon] => 
                            [MenuLink] => 
                            [MenuModule] => "ModulMenu1"
                        )
                    [1] => Array
                        (
                            [MenuId] => 7
                            [MenuName] => Sub Menu 2
                            [MenuIcon] => 
                            [MenuLink] => 
                            [MenuModule] => "ModulMenu2"
                        )
                )

        )
    [1] => Array
        (
            [MenuId] => 2
            [MenuName] => Configuration
            [MenuAttribute] => 
            [MenuChild] => Array
                (
                    [0] => Array
                        (
                            [MenuId] => 14
                            [MenuName] => Sub Menu 2-1
                            [MenuIcon] => 
                            [MenuLink] => 
                            [MenuModule] => "ModulMenu2-1"
                        )
                    [1] => Array
                        (
                            [MenuId] => 15
                            [MenuName] => Sub Menu 2-2
                            [MenuIcon] => 
                            [MenuLink] => 
                            [MenuModule] => "ModulMenu2-2"
                        )
                    [2] => Array
                        (
                            [MenuId] => 16
                            [MenuName] => Sub Menu 2-3
                            [MenuIcon] => 
                            [MenuLink] => 
                            [MenuModule] => "ModulMenu2-3"
                        )
                )
        )
)

Here is an example of the data I mean..

thank you.

Create a class

    public class MenuItems
    {
         public int MenuId { get; set; }
         public string MenuName { get; set; }
         public string MenuAttribute { get; set; }
         public List<MenuItems> MenuChild { get; set; }
    }

Then use it like

   List<MenuItems> data = new List<MenuItems>();

and fill the list in nested loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM