简体   繁体   English

没有实体框架的列表视图/详细信息视图

[英]List View / Details View without Entity Framework

I have a MVC view that has a table that contains a list of all users within a specific OU within Active Directory. 我有一个MVC 视图 ,该视图具有一个表,该表包含Active Directory中特定OU内的所有用户的列表。 I am trying to add a column to the table that takes you to a Details view that shows the details of the user (shows additional details) but I am having a hard time figuring out how to do this without using Entity Framework and passing the id of the object to the details view. 我正在尝试向表中添加一列,以将您带到显示用户详细信息的“详细信息”视图(显示其他详细信息),但是我很难弄清楚如何在不使用实体框架的情况下执行此操作并传递ID对象的详细信息视图。 Any thoughts on how I could accomplish this? 关于如何实现此目标有任何想法吗? Right now when I click on the Details actionlink, I am taken to the Details view but there is no data being passed. 现在,当我单击“详细信息”操作链接时,将进入“详细信息”视图,但是没有任何数据正在传递。 Any help would be greatly appreciated! 任何帮助将不胜感激!

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>

There are a couple ways to do this. 有两种方法可以做到这一点。 One of which is the way you are already doing it. 其中一种就是您已经在做的方式。 You can add all the properties you want to display on your details page to your User model. 您可以将要显示在详细信息页面上的所有属性添加到User模型。 Use that list to build your table, then pass the applicable User object back to your Details view to display the extra details. 使用该列表构建表,然后将适用的User对象传递回“ Details视图以显示其他详细信息。

That method is good because it saves you having to do a second lookup to AD for your Details view (essentially, your ????????? can be removed - you don't need any code there). 该方法很好,因为它省去了第二次查询AD的“ Details视图的操作(基本上,可以删除????????? -那里不需要任何代码)。

I was going to say that the down side is that, in your search, you're asking for more details for most of the accounts than you'd actually use, but the UserPrincipal objects that PrincipalSearcher returns already pulls in all the AD attributes for each user account anyway. 我要说的UserPrincipal是,在搜索中,您要求提供的大多数帐户的详细信息都比实际使用的要多,但是PrincipalSearcher返回的UserPrincipal对象已经获取了所有AD属性。无论如何,每个用户帐户。 If you used DirectorySearcher directly, you'd have more control of that. 如果直接使用DirectorySearcher ,则可以对其进行更多控制。 But that's probably not a big deal, unless you have performance problems with that search. 但这可能不是什么大问题,除非您对该搜索有性能问题。

The other way to do it, which I think is unnecessary in your current example, is to store some unique identifier from AD (eg distinguishedName , sAMAccountName , userPrincipalName , objectGuid , objectSid ) and pass just that back to your Details action where you can look up just that account again and get all the details you need for your Details view. 另一种方法,我认为在当前示例中是不必要的,它是存储AD中的一些唯一标识符(例如, distinguishedNamesAMAccountNameuserPrincipalNameobjectGuidobjectSid ),并将其仅传递回Details动作,您可以在其中查看重新建立该帐户,并获取“详细信息”视图所需的所有详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM