我想创建一个新的 Action 方法,当调用它时将返回所有 Active Directory 用户名。asp.net mvc 应用程序和活动目录在同一个域中(并且当前在同一个开发机器中)。所以我定义了以下动作方法:-
public ViewResult Details()
{
var c = repository.GetUserDetails3();
return View("Details2",c); }
以及以下存储库方法:-
public DirectoryEntry GetUserDetails3()
{ DirectoryEntry de = new DirectoryEntry();
using (var context = new PrincipalContext(ContextType.Domain, "WIN-SPDEV.com")){using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{foreach (var result in searcher.FindAll())
{de = result.GetUnderlyingObject() as DirectoryEntry; }}}
return de; }
和以下模型类:-
public class DirectoryUser
{public Nullable<Guid> Guid { get; set; }
public string Name { get; set; }
public string Username { get; set; }
}}
在我看来:-
@model IEnumerable<TMS.Models.DirectoryUser>
@{
ViewBag.Title = "Details2";
}
<h2>Details2</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Guid)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Username)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Guid)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Username)
</td>
但是当我调用操作方法时,出现以下错误:-参数字典包含不可为空类型的参数“id”的空条目
传递到字典中的模型项的类型为“System.DirectoryServices.DirectoryEntry”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[TMS.Models.DirectoryUser]”的模型项。