C# 如何在asp.net mvc中将字典值绑定到列

C# 如何在asp.net mvc中将字典值绑定到列,c#,asp.net,asp.net-mvc,asp.net-mvc-3,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,Razor,我正在使用Active Directory身份验证库获取Active Directory中的用户列表。视图已绑定到此库中的用户类 @using Microsoft.Azure.ActiveDirectory.GraphClient @model IEnumerable<User> @{ ViewBag.Title = "Index"; Layout = "~/Areas/GlobalAdmin/Views/Shared/_LayoutGlobalAdmin.csh

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

@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>
@使用Microsoft.Azure.ActiveDirectory.GraphClient
@模型IEnumerable
@{
ViewBag.Title=“Index”;
Layout=“~/Areas/GlobalAdmin/Views/Shared/\u layoutlobaladmin.cshtml”;
}
惯常
乌萨里奥斯酒店
@ActionLink(“新建”、“创建”、null、新建{@class=“btn btn primary btn xs”})
用户名
显示名称
职务
目标
@foreach(模型中的var项目)
{
var user=作为用户的项目;
@DisplayFor(modelItem=>user.UserPrincipalName)
@DisplayFor(modelItem=>user.DisplayName)
@DisplayFor(modelItem=>user.JobTitle)
@DisplayFor(modelItem=>user.ObjectId)
@ActionLink(“编辑”,“编辑”,新的{objectId=item.objectId})
@ActionLink(“详细信息”,“详细信息”,新的{objectId=item.objectId})
@ActionLink(“删除”,“删除”,新建{objectId=item.objectId})
@ActionLink(“GroupMembership”,“GetGroups”,new{objectId=item.objectId})
@ActionLink(“DirectReports”、“GetDirectReports”,new{objectId=item.objectId})
}
但是,在Active Directory中,您可以创建自定义属性,这些属性称为架构扩展: 仅供参考

在我的用户列表中,有些用户具有自定义属性,而有些用户没有

在我的视图中,我想创建一列来显示该自定义属性(如果存在)的值

我知道如何获取服务器端的值,但我不知道如何将其绑定到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);
    }
公共异步任务索引()
{
var userList=新列表();
尝试
{
ActiveDirectoryClient=AuthenticationHelper.GetActiveDirectoryClient();
IPagedCollection pagedCollection=await client.Users.ExecuteAsync();
如果(pagedCollection!=null)
{
做
{
List usersList=pagedCollection.CurrentPage.ToList();
foreach(用户列表中的IUser用户)
{
var usuarioAD=(Microsoft.Azure.ActiveDirectory.GraphClient.User)用户;
var extendedProperties=usuarioAD.GetExtendedProperties();
foreach(extendedProperties中的var属性)
{
if(property.Key.Contains(“Compania”))
{
字符串值=property.value.ToString();
}
}
添加((Microsoft.Azure.ActiveDirectory.GraphClient.User)用户);
}
pagedCollection=等待pagedCollection.GetNextPageAsync();
}while(pagedCollection!=null);
}
}
捕获(例外e)
{
if(Request.QueryString[“reauth”]=“True”)
{
//
//发送OpenID连接登录请求以获取一组新令牌。
//如果用户仍然拥有与Azure AD的有效会话,则不会提示他们输入凭据。
//处理登录响应后,OpenID Connect中间件将返回到此控制器。
//
HttpContext.GetOwinContext()
.Authentication.Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
//
//用户需要重新授权。向他们显示一条这样的消息。
//
ViewBag.ErrorMessage=“需要授权”;
返回视图(用户列表);
}
返回视图(用户列表);
}

如何绑定该值?或者我可以在razor视图上写一个复杂的表达式,而不必在控制器上写任何东西来获得该值?

我觉得你的问题和名字听起来很熟悉,这时我意识到我之前已经给出了答案。我认为这个问题与你的另一个问题有关

我不太明白你的
索引
控制器是做什么的,但这在回答你的问题时可能并不太重要

我不会对您的扩展属性使用Dictionary属性。相反,我会使用对象的集合,比如
ExtendedProperty
类,它由两个属性组成:
Name
Value

所以你的
用户
public IList<ExtendedProperty> ExtendedProperties
@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)
        }
    }
}