title: ASP.NET MVC5 - DropDownList with Active Directory Users
body: <p>I am new to ASP.NET MVC and I got stuck when I try to post Users from Active Directory to my SQL Database.</p>
  
  <p>This is what I got so far:</p>
  
  <p>ViewModel:</p>
  
  <pre><code> public class UserViewModel
  {
      public IEnumerable&lt;SelectListItem&gt; myADUsers { get; set; }
      public int SelectedPersonId { get; set; }
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>public ActionResult Index()
      {
          UserViewModel myADUsers = new UserViewModel();           
          myADUsers.myADUsers = GetADUsers();
  
          return View(myADUsers);
  
      }
  
  public IEnumerable&lt;SelectListItem&gt; GetADUsers()
      {
          List&lt;SelectListItem&gt; _users = new List&lt;SelectListItem&gt;();
          //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&lt;SelectListItem&gt; myADUsers = _users;
  
          return myADUsers;
      }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>@Html.DropDownList("My AD Users", Model.myADUsers, "Please select a user")
  </code></pre>
  
  <p>This works fine, as my DropDownList is populated with my Users from Active Directory but how do I get the selected User?</p>
  
  <p>I'd be very grateful to get some tipps.. Thank you.</p>