gpt4 book ai didi

c# - ASP.NET MVC5 - 带有 Active Directory 用户的 DropDownList

转载 作者:太空宇宙 更新时间:2023-11-03 12:24:18 24 4
gpt4 key购买 nike

我是 ASP.NET MVC 的新手,当我尝试将用户从 Active Directory 发布到我的 SQL 数据库时遇到了困难。

这是我到目前为止得到的:

View 模型:

 public class UserViewModel
{
public IEnumerable<SelectListItem> myADUsers { get; set; }
public int SelectedPersonId { get; set; }
}

Controller :

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 的用户,但我如何获得选定的用户?

如果能得到一些小费,我将不胜感激。谢谢。

最佳答案

只需将 DropDownList 放入 Html.BeginForm 中,如下所示:

  @using (Html.BeginForm("Method", "Controller", FormMethod.Post))
{

@Html.DropDownList("users", Model.myADUsers, "Please select a user")
<button type="submit">Submit</button>
}

然后像这样在您的方法中接收用户名:

[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult Method(string users)
{
//do stuff here
}

关于c# - ASP.NET MVC5 - 带有 Active Directory 用户的 DropDownList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45855835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com