[Solved] MVC AD search – Displaying multiple results as .cshtml

hayhay Asks: MVC AD search – Displaying multiple results as .cshtml
I have fully working code that searches active directory and displays it with MVC .cshtml But I have been trying to figure out away to add all the users found to a list then display them. As currently it just displays the first user found.

This is the HomeController that takes a value, Searches AD and returns the results.

Code:
    public class HomeController : Controller
    {
        public ActionResult Index(IndexViewModel profile)
        {
            if (ModelState.IsValid)
            {
                //List<Principal> users = new List<Principal>();
                using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
                {
                    UserPrincipal qbeUser = new UserPrincipal(ctx);
                    qbeUser.DisplayName = profile.Name + "*";

                    using (PrincipalSearcher srch = new PrincipalSearcher(qbeUser))
                    {
                        if(!(srch.FindAll().Count() < 0))
                        {
                            foreach(var found in srch.FindAll())
                            {
                                //users.Add(found);
                                IndexViewModel returnmodel = new IndexViewModel(found);

                                return View(returnmodel);
                            }
                        }                       
                    }
                }                               
            }
            return View(profile);
        }
   }

The IndexViewModel

Code:
public class IndexViewModel
    {

        public IndexViewModel(Principal found)
        {
            Name = found.DisplayName;
            Email = found.UserPrincipalName;
            Description = found.Description;

        }

        [Required(ErrorMessage = "Please enter a name")]
        [Display(Name = "Persons Name")]
        public string Name { get; set; }
        public string Email { get; set; }
        public string Description { get; set; }
        //public List<Principal> user { get; set; }

    }

Index.cshtml

Code:
    <div id="content">
    @Html.ValidationSummary(true)
    @using (Html.BeginForm("Index", "Home"))
    {


        <fieldset>

            <div class="form-group col-md-12">
                @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
                <div class="col-md-4">
                    @Html.EditorFor(modelItem => Model.Name, new { htmlAttributes = new { @class = "form-control", @style = "width:280px" }, })
                    @Html.ValidationMessageFor(x => x.Name)
                </div>
                <div class="col-md-2">
                    <input type="submit" class="btn btn-default" value="Search">
                </div>
                <div class="col-md-3">

                </div>

            </div>
        </fieldset>
    }
    <br>
</div>
    <table id="historyTable" class="table">
     <thead>
        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>@Model.Name</td>
            <td>@Model.Email</td>
            <td>@Model.Description</td>
            </tr>
    </tbody>
</table>

EDIT———– This is one method I tried —————-

HomeController.cs

Code:
public class HomeController : Controller
{
    public ActionResult Index(IndexViewModel profile)
    {
        if (ModelState.IsValid)
        {
            List<Principal> users = new List<Principal>();
            using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
            {
                UserPrincipal qbeUser = new UserPrincipal(ctx);
                qbeUser.DisplayName = profile.Name + "*";

                using (PrincipalSearcher srch = new PrincipalSearcher(qbeUser))
                {
                    if(!(srch.FindAll().Count() < 0))
                    {
                        foreach(var found in srch.FindAll())
                        {
                            users.Add(found);
                            IndexViewModel returnmodel = new IndexViewModel(users);
                            return View(returnmodel);
                        }
                    }                       
                }
            }                               
        }
        return View(profile);
    }

IndexViewModel.cs

Code:
public class IndexViewModel
{

    public IndexViewModel(List<Principal> found)
    {
        user = found;
    }

    [Required(ErrorMessage = "Please enter a name")]
    [Display(Name = "Persons Name")]
    public string Name { get; set; }
    public string Email { get; set; }
    public string Description { get; set; }
    public List<Principal> user { get; set; }

}

index.html

Code:
<div id="content">
    @Html.ValidationSummary(true)
    @using (Html.BeginForm("Index", "Home"))
    {


        <fieldset>

            <div class="form-group col-md-12">
                @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
                <div class="col-md-4">
                    @Html.EditorFor(modelItem => Model.Name, new { htmlAttributes = new { @class = "form-control", @style = "width:280px" }, })
                    @Html.ValidationMessageFor(x => x.Name)
                </div>
                <div class="col-md-2">
                    <input type="submit" class="btn btn-default" value="Search">
                </div>
                <div class="col-md-3">

                </div>

            </div>
        </fieldset>
    }
    <br>
</div>
    <table id="historyTable" class="table">
     <thead>
        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        @using System.DirectoryServices.AccountManagement
        @foreach (Principal prin in Model.user)
        {
            <tr>
                <td>@prin.DisplayName</td>
                <td>@prin.UserPrincipalName</td>
                <td>@prin.Description</td>
             </tr>
        }        
    </tbody>
</table>

The error I get on compile is —

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Code:
Line 37:     <tbody>
Line 38:         @using System.DirectoryServices.AccountManagement
Line 39:         @foreach (Principal prin in Model.user)
Line 40:         {
Line 41:             <tr>

Source File: C:UsershgaDocumentsVisual Studio 2015ProjectsIntra AD people searcherIntra AD people searcherViewsHomeIndex.cshtml Line: 39

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.