Created a class in the model to retrieve data from AD
namespace NetJumper_03.Models { public class ActiveDirectory { public string DisplayName { set; get; } public string SAMAccountName { set; get; } public string SN { set; get; } public string GivenName { set; get; } public string Company { set; get; } public string Department { set; get; } public string Title { set; get; } public string PhysicalDeliveryOfficeName { set; get; } public string Mail { set; get; } public string TelephoneNumber { set; get; } public string Mobile { set; get; } public static IEnumerable<ActiveDirectory> GetUserFromAD() { var newUser = new ActiveDirectory(); using (var context = new PrincipalContext(ContextType.Domain, "office.local")) { using (var searcher = new PrincipalSearcher(new UserPrincipal(context))) { foreach (var result in searcher.FindAll()) { DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry; newUser.DisplayName = (String)de.Properties["displayName"].Value; newUser.SAMAccountName = (String)de.Properties["sAMAccountName"].Value; newUser.SN = (String)de.Properties["sn"].Value; newUser.GivenName = (String)de.Properties["givenName"].Value; newUser.Company= (String)de.Properties["company"].Value; newUser.Department = (String)de.Properties["department"].Value; newUser.Title = (String)de.Properties["mobile"].Value; newUser.Mail = (String)de.Properties["title"].Value; newUser.TelephoneNumber = (String)de.Properties["physicalDeliveryOfficeName"].Value; newUser.Mobile = (String)de.Properties["mail"].Value; } } return (IEnumerable<ActiveDirectory>)newUser; } } } } The controller looks like this
public ActionResult Index() { return View(ActiveDirectory.GetUserFromAD()); } In a form like this
@model IEnumerable<NetJumper_03.Models.ActiveDirectory> @{ ViewBag.Title = "Index"; } <h2>Index</h2> @foreach(var items in Model) { <p>@items.DisplayName</p> } All anything, but gives an error
Additional information: Could not cast object type "NetJumper_03.Models.ActiveDirectory" to type "System.Collections.Generic.IEnumerable`1
I understand that I have sinned here return (IEnumerable<ActiveDirectory>)newUser;
Explain what to do?