What I have done is search Activedirectory users with a given value being a name. I then create a viewmodel that contains the values Name,Email and description. I then display it as .cshtml on the index.

The thing is with the way I have made it, it only sends through the first user it finds- (If I search for Andrew out of multiple Andrews, it finds them all but returns the first one.)

I want to add them to a list then pretty much do the same thing, but of course on the .cshtml iterate over the list and display the results.

Here is the HomeController.cs code --

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 bit to look at in that is the

foreach(var found in srch.FindAll())
                        {
                            //users.Add(found);
                            IndexViewModel returnmodel = new IndexViewModel(found);


                            return View(returnmodel);
                        }

Here is the code for the IndexViewModel.cs#

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; }

}

And here is the Index.cshtml

/ this just create the input box, validation text and submit button.

<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>

This here displays the single result found /

<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>

Related posts

Recent Viewed