【问题标题】:List View / Details View without Entity Framework没有实体框架的列表视图/详细信息视图
【发布时间】:2018-10-30 22:07:02
【问题描述】:

我有一个 MVC view,它有一个表,其中包含 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 model 参数。您的 Details 方法需要一个 int id 参数,并且“详细信息”链接会将用户的 ID 传递给该方法
  • 由于用户信息来自 Active Directory,因此没有为用户设置 ID,如果我从这两种方法中删除用户模型,那么我无法将任何用户信息从列表视图传递到在详细信息视图中,我只是得到“对象引用未设置为对象的实例。”。不过感谢您的意见!
  • Active Directory 用户确实有唯一的 ID - 例如,sAMAccountName

标签: c# asp.net-mvc active-directory directoryservices directoryentry


【解决方案1】:

有几种方法可以做到这一点。其中之一是你已经在做的方式。您可以将要在详细信息页面上显示的所有属性添加到 User 模型。使用该列表构建您的表格,然后将适用的 User 对象传递回您的 Details 视图以显示额外的详细信息。

这种方法很好,因为它使您不必为您的Details 视图再次查找 AD(本质上,您的 ????????? 可以被删除 - 您不需要任何代码)。

我要说的缺点是,在您的搜索中,您要求的大多数帐户的详细信息比您实际使用的要多,但 PrincipalSearcher 已经返回的 UserPrincipal 对象无论如何都会为每个用户帐户提取所有 AD 属性。如果您直接使用DirectorySearcher,您将拥有更多的控制权。但这可能没什么大不了的,除非您对该搜索有性能问题。

另一种方法,我认为在您当前的示例中是不必要的,是从 AD 存储一些唯一标识符(例如 distinguishedNamesAMAccountNameuserPrincipalNameobjectGuidobjectSid)并将其传递回您的 Details 操作,您可以在其中再次查找该帐户并获取详细信息视图所需的所有详细信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    相关资源
    最近更新 更多