如何将字典值绑定到 asp.net mvc 中的列

How to bind a dictionary value to a column in asp.net mvc

我正在使用 Active Directory 身份验证库获取我的 Active Directory 中的用户列表。视图绑定到此库中的用户 class。

@using Microsoft.Azure.ActiveDirectory.GraphClient
@model IEnumerable<User>


@{
    ViewBag.Title = "Index";
    Layout = "~/Areas/GlobalAdmin/Views/Shared/_LayoutGlobalAdmin.cshtml";
}
<h2>Usuarios</h2>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>Lista de Usuarios</h5>
                    <div class="ibox-tools">
                        @Html.ActionLink("Create New", "Create", null, new { @class = "btn btn-primary btn-xs" })
                    </div>
                </div>
                <div class="ibox-content">

                    <table id="directoryObjects" class="table table-bordered table-striped">
                        <tr>
                            <th>
                                UserPrincipalName
                            </th>
                            <th>
                                DisplayName
                            </th>
                            <th>
                                JobTitle
                            </th>
                            <th>
                                ObjectId
                            </th>
                            <th />
                        </tr>
                        @foreach (var item in Model)
    {
        var user = item as User;
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => user.UserPrincipalName)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => user.DisplayName)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => user.JobTitle)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => user.ObjectId)
                            </td>
                            <td>
                                @Html.ActionLink("Edit", "Edit", new { objectId = item.ObjectId }) <br />
                                @Html.ActionLink("Details", "Details", new { objectId = item.ObjectId }) <br />
                                @Html.ActionLink("Delete", "Delete", new { objectId = item.ObjectId })  <br />
                                @Html.ActionLink("GroupMembership", "GetGroups", new { objectId = item.ObjectId }) <br />
                                @Html.ActionLink("DirectReports", "GetDirectReports", new { objectId = item.ObjectId }) <br />
                            </td>
                        </tr>
    }
                    </table>

                </div>
            </div>
        </div>
    </div>
</div>

但是在 Active Directory 中您可以创建自定义属性,这些属性称为架构扩展: 仅供参考 http://justazure.com/azure-active-directory-part-6-schema-extensions/

在我的用户列表中,有些用户拥有自定义属性,有些则没有。

在我看来,我想创建一个列来显示该自定义值 属性(如果存在)。

我知道如何获取服务器端的值,但我不知道如何将它绑定到 HTML

这是用户控制器中的索引操作

 public async Task<ActionResult> Index()
    {
        var userList = new List<Microsoft.Azure.ActiveDirectory.GraphClient.User>();
        try
        {
            ActiveDirectoryClient client = AuthenticationHelper.GetActiveDirectoryClient();
            IPagedCollection<IUser> pagedCollection = await client.Users.ExecuteAsync();
            if (pagedCollection != null)
            {
                do
                {
                    List<IUser> usersList = pagedCollection.CurrentPage.ToList();
                    foreach (IUser user in usersList)
                    {
                        var usuarioAD = (Microsoft.Azure.ActiveDirectory.GraphClient.User)user;
                        var extendedProperties = usuarioAD.GetExtendedProperties();

                        foreach (var property in extendedProperties)
                        {
                            if (property.Key.Contains("Compania"))
                            {
                                string value = property.Value.ToString();
                            }
                        }

                        userList.Add((Microsoft.Azure.ActiveDirectory.GraphClient.User)user);
                    }
                    pagedCollection = await pagedCollection.GetNextPageAsync();
                } while (pagedCollection != null);
            }
        }
        catch (Exception e)
        {
            if (Request.QueryString["reauth"] == "True")
            {
                //
                // Send an OpenID Connect sign-in request to get a new set of tokens.
                // If the user still has a valid session with Azure AD, they will not be prompted for their credentials.
                // The OpenID Connect middleware will return to this controller after the sign-in response has been handled.
                //
                HttpContext.GetOwinContext()
                    .Authentication.Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType);
            }

            //
            // The user needs to re-authorize.  Show them a message to that effect.
            //
            ViewBag.ErrorMessage = "AuthorizationRequired";
            return View(userList);
        }
        return View(userList);
    }

如何绑定值?或者也许我可以在剃刀视图上写一个复杂的表达式而不在控制器上写任何东西来获得那个值?

我觉得你的问题和名字听起来很熟悉,那时我才意识到我之前已经回答了 。我认为这里的这个问题与您提出的其他问题有关。

我不太明白你的 Index 控制器的作用,但它在回答你的问题时可能不太重要。

我不会为您的扩展属性使用字典 属性。相反,我会使用对象的集合,比如 ExtendedProperty class,它由两个属性组成:NameValue.

所以你的 User class 会是这样的:

public IList<ExtendedProperty> ExtendedProperties

在您看来,您会这样做...请注意,我会将您的 foreach 循环替换为 for 循环。

@for(int a = 0; a < Model.Count(); a++)
{
    var user = Model[a];
    ...

    @if (user.ExtendedProperties != null
            && user.ExtendedProperties.Count() > 0) {
        for (int i = 0; i < user.ExtendedProperties.Count(); i++)
        {
            @Html.DisplayFor(m => m[a].ExtendedProperties[i].Name)
            @Html.DisplayFor(m => m[a].ExtendedProperties[i].Value)
        }
    }
}