si2030
si2030

Reputation: 4035

Asp.net MVC Populating an object that has a list item with data

I have looked and not found a specific example of this.

I have a class that has as part of it a list of roles.

public class MenuItem
{
    public int Id { get; set; }
    public bool Divider { get; set; }
    public bool Header { get; set; }
    public string ActionName { get; set; }
    public string ControllerName { get; set; }
    public string MenuItemText { get; set; }
    public string Title { get; set; }
    public IList<string> Roles { get; set; }
    public int ParentId { get; set; }
}

In it, it has Public "IList Roles {...}"

I want to add items to a list of MenuItems..

I wanted to use the ".Add" eg:

MenuList = new MenuItemList();

MenuList.MenuItems.Add(new MenuItem(1, "Scheduling", false, true, "Index", "Scheduler", "Scheduling", ??? )

and thats fine for the ints bools and strings but how do I add a list of roles inline to this.. I would like to add a list of roles as part of this ".Add"... line?

Upvotes: 0

Views: 138

Answers (2)

Nitin Varpe
Nitin Varpe

Reputation: 10694

Try this

var list = new List<string>(){"Admin","Normal"};

MenuList.MenuItems.Add(new MenuItem(1, "Scheduling", false, true, "Index", "Scheduler", "Scheduling"
, list  )// You need to add list of string here.

Upvotes: 1

Borja Tur
Borja Tur

Reputation: 817

A working example with a little diferent sintax

public class MenuItem
{
     public int Id { get; set; }
     public bool Divider { get; set; }
     public bool Header { get; set; }
     public string ActionName { get; set; }
     public string ControllerName { get; set; }
     public string MenuItemText { get; set; }
     public string Title { get; set; }
     public IList<string> Roles { get; set; }
     public int ParentId { get; set; }
}
var MenuList = new List<MenuItem>();
var menuItem = new MenuItem
{
    Id = 1,
    Divider = true,
    Header = true,
    ActionName = "Scheduling",
    ControllerName = "Index",
    MenuItemText = "sdfs",
    Title = "Scheduling",
    Roles = new List<string>{"rol1", "rol2"},
    ParentId = 30
};
MenuList.Add(menuItem);

Upvotes: 0

Related Questions