[英]How to bind a dictionary value to a column in asp.net mvc
我正在使用Active Directory身份验证库来获取我的活动目录中的用户列表。 该视图绑定到此库中的User类。
@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);
}
如何绑定值? 还是我可以在razor视图上写一个复杂的表达式而无需在控制器上写任何东西来获取该值?
我以为您的问题和名字听起来很熟悉,那是在我意识到我早些时候给您答案的时候。 我认为这里的问题与您遇到的其他问题有关。
我不太了解您的Index
控制器的功能,但是回答您的问题可能不太重要。
我不会将Dictionary属性用于您的扩展属性。 相反,我将使用ExtendedProperty
类的对象集合,该对象集合包含两个属性: Name
和Value
。
因此,您的User
类将具有以下内容:
public IList<ExtendedProperty> ExtendedProperties
在您看来,您可以这样做...注意,我将用for
循环替换您的foreach
循环。
@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)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.