.net mvc selectlist多个来源

时间:2012-11-27 15:38:48

标签: asp.net-mvc active-directory

我有一个我从Active Directory中检索的用户列表,如此

PrincipalContext pc = new PrincipalContext(ContextType.Domain, "DOMAIN", "dc=domain,dc=org");
GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, "Advisors");
ViewBag.EmployeeID = new SelectList(group.Members, "EmployeeID", "DisplayName");

我想在此列表中添加两个附加项,但不想在Active Directory中为它们创建用户帐户,因为它们不是真正的用户。

是否可以执行类似

的操作
var items1 = new[] 
{  
    new { EmployeeID = "1", DisplayName = "Independent" },
    new { EmployeeID = "2", DisplayName = "Own" }
};

var items = Concat(group.Members, items1);
{
    ViewBag.EmployeeID = new SelectList(items, "EmployeeID", "DisplayName");
}

1 个答案:

答案 0 :(得分:1)

嗯,你可以这样ViewModel

public class EmployeeViewModel
{
    public string EmployeeId { get; set; }
    public string DisplayName { get; set; }
}

并且您可以将该组转换为。您可以在ViewModel列表中添加一些自定义itens并将此列表设置为ViewBag。

var members = group.Members.Select(x => new EmployeeViewModel() { EmployeeId = x.EmployeeId, DisplayName = x.DisplayName }).ToList();

members.Add(new EmployeeViewModel() { EmployeeId = "1", DisplayName = "Independent" });
members.Add(new EmployeeViewModel() { EmployeeId = "2", DisplayName = "Own" });

ViewBag.EmployeeID = new SelectList(members, "EmployeeID", "DisplayName");