我有完全工作的代码,可以搜索活动目录并使用MVC .cshtml显示它,但是我一直在努力找出将找到的所有用户添加到列表中然后显示它们的方法。目前它只显示找到的第一个用户。
这是采用值"搜索 AD "并返回结果的 HomeController 。
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);
}
}
索引视图模型
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
<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>
编辑-----------这是我尝试过的一种方法----------------
首页控制器.cs
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);
}
索引视图模型.cs
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; }
}
索引.html
<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>
我在编译时得到的错误是——
对象引用未设置为对象的实例。 说明:执行当前 Web 请求期间发生未经处理的异常。请查看堆栈跟踪,了解有关错误及其在代码中起源位置的详细信息。
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
源错误:
Line 37: <tbody>
Line 38: @using System.DirectoryServices.AccountManagement
Line 39: @foreach (Principal prin in Model.user)
Line 40: {
Line 41: <tr>
源文件: C:\Users\hga\Documents\Visual Studio 2015\Projects\Intra AD 人员搜索器\Intra AD 人员搜索器\视图\主页\Index.cshtml 行: 39
您可以添加 if 语句来检查空
值@if(Model.user !=null)
{
@foreach (Principal prin in Model.user)
{
<!--your code here-->
}
}
在控制器中,return 语句位于 foreach 循环中。所以它第一次通过循环时,它会返回。这意味着您只会有一个结果。
试试这个:
foreach(var found in srch.FindAll())
{
users.Add(found);
}
IndexViewModel returnmodel = new IndexViewModel(users);
return View(returnmodel);