我是ASP.NET MVC的新手,當我嘗試將用戶從Active Directory發佈到我的SQL數據庫時,我陷入了困境。ASP.NET MVC5 - 使用Active Directory用戶的DropDownList
這是我走到這一步:
視圖模型:
public class UserViewModel
{
public IEnumerable<SelectListItem> myADUsers { get; set; }
public int SelectedPersonId { get; set; }
}
控制器:
public ActionResult Index()
{
UserViewModel myADUsers = new UserViewModel();
myADUsers.myADUsers = GetADUsers();
return View(myADUsers);
}
public IEnumerable<SelectListItem> GetADUsers()
{
List<SelectListItem> _users = new List<SelectListItem>();
//Instantiate Active Directory Server Connection
PrincipalContext adServer = new PrincipalContext(ContextType.Domain, null);
//Instantiate Active Directory User Group
GroupPrincipal adGroup = GroupPrincipal.FindByIdentity(adServer, "XXXXX");
if (adGroup != null)
{
foreach (Principal p in adGroup.GetMembers())
{
_users.Add(new SelectListItem { Text = p.SamAccountName, Value = p.SamAccountName });
}
}
IEnumerable<SelectListItem> myADUsers = _users;
return myADUsers;
}
查看:
@Html.DropDownList("My AD Users", Model.myADUsers, "Please select a user")
這工作得很好,因爲我的DropDownList被填充我的用戶來自Active Directory,但我如何獲得選定的用戶?
我非常感謝能得到一些tipps ..謝謝。
你的意思是你要發送的名字的用戶到你的控制器方法? –
感謝您的及時答覆。是的,我想將選定的用戶名發回給我想填充我的數據庫模型(EF6)的UserName屬性的Controller。 – exmachina
很高興,它幫助你 –