简体   繁体   English

Asp.net MVC填充具有列表项和数据的对象

[英]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 {...}" 其中具有公开的“ IList角色{...}”

I want to add items to a list of MenuItems.. 我想将项目添加到MenuItems列表中。

I wanted to use the ".Add" eg: 我想使用“ .Add”,例如:

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? 并且这对于int布尔和字符串来说还不错,但是如何在此内联添加角色列表。我想在“ .Add” ...行中添加角色列表?

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.

A working example with a little diferent sintax 一个具有不同的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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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