我所做的是以给定值作为名称来搜索 Activedirectory 用户。然后,我创建一个包含名称、电子邮件和描述值的视图模型。然后我在索引上将其显示为 .cshtml。
问题在于我的制作方式,它只发送它找到的第一个用户-(如果我从多个安德鲁中搜索安德鲁,它会找到所有用户,但会返回第一个用户。)
我想将它们添加到列表中,然后几乎做同样的事情,但当然在 .cshtml 上迭代列表并显示结果。
这是 HomeController.cs 代码——
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);
}
需要注意的是
foreach(var found in srch.FindAll())
{
//users.Add(found);
IndexViewModel returnmodel = new IndexViewModel(found);
return View(returnmodel);
}
这是 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; }
}
这是 Index.cshtml
/ 这只是创建输入框、验证文本和提交按钮。
<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>