[英]List View / Details View without Entity Framework
我有一个MVC 视图 ,该视图具有一个表,该表包含Active Directory中特定OU内的所有用户的列表。 我正在尝试向表中添加一列,以将您带到显示用户详细信息的“详细信息”视图(显示其他详细信息),但是我很难弄清楚如何在不使用实体框架的情况下执行此操作并传递ID对象的详细信息视图。 关于如何实现此目标有任何想法吗? 现在,当我单击“详细信息”操作链接时,将进入“详细信息”视图,但是没有任何数据正在传递。 任何帮助将不胜感激!
public ActionResult NonComputerUsers(User model)
{
List<User> user = new List<User>();
using (var context = new PrincipalContext(ContextType.Domain, "XXX", "OU=XXX,DC=XXX,DC=com"))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
foreach (var result in searcher.FindAll())
{
DirectoryEntry entry = result.GetUnderlyingObject() as DirectoryEntry;
user.Add(new User()
{
FirstName = (String)entry.Properties["givenName"].Value,
LastName = (String)entry.Properties["sn"].Value,
});
}
}
}
return View(user.ToList());
}
public ActionResult Details(User model)
{
?????????
return View(model);
}
**List View**
@model ADUserManagement.Models.User
<table class="table">
<tr>
<th>
First Name
</th>
<th>
Last Name
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.ActionLink("Details", "Details", "Users", new { item = item.FirstName })
</td>
</tr>
}
**Details View**
@model ADUserManagement.Models.User
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<table class="table table-bordered table-striped">
<tr>
<td>First Name</td>
<td>@Model.FirstName</td>
</tr>
<tr>
<td>Last Name</td>
<td>@Model.LastName</td>
</tr>
<tr>
<td>SAM Account Name</td>
<td>@Model.SamAccountName</td>
</tr>
有两种方法可以做到这一点。 其中一种就是您已经在做的方式。 您可以将要显示在详细信息页面上的所有属性添加到User
模型。 使用该列表构建表,然后将适用的User
对象传递回“ Details
视图以显示其他详细信息。
该方法很好,因为它省去了第二次查询AD的“ Details
视图的操作(基本上,可以删除?????????
-那里不需要任何代码)。
我要说的UserPrincipal
是,在搜索中,您要求提供的大多数帐户的详细信息都比实际使用的要多,但是PrincipalSearcher
返回的UserPrincipal
对象已经获取了所有AD属性。无论如何,每个用户帐户。 如果直接使用DirectorySearcher
,则可以对其进行更多控制。 但这可能不是什么大问题,除非您对该搜索有性能问题。
另一种方法,我认为在当前示例中是不必要的,它是存储AD中的一些唯一标识符(例如, distinguishedName
, sAMAccountName
, userPrincipalName
, objectGuid
, objectSid
),并将其仅传递回Details
动作,您可以在其中查看重新建立该帐户,并获取“详细信息”视图所需的所有详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.