0

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?

2 Answers 2

1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

From @Nitin 's answer I was able to construct this: MenuList.MenuItems.Add(new MenuItem(1, false, true, "Index", "Scheduler", "Scheduling", new List<string>() { "Admin", "Normal" }, 0)); which solves my problem.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.