【发布时间】:2019-02-19 05:12:36
【问题描述】:
我正在尝试在视图中显示活动目录组成员。当我运行代码时出现错误“传递到字典中的模型项的类型为‘System.String[]’,但该字典需要‘System.Collections.Generic.IEnumerable`1[SSSFT’类型的模型项.LogIT.Models.ActiveDirectory]'"。调试代码显示我正在寻找的所有组成员
模型类
public class ActiveDirectory
{
public int id { get; set; }
//public string Username { get; set; }
//public string Email { get; set; }
public string SamAccountName { get; set; }
}
控制器
public ActionResult Index(string username)
{
username = "sssftappdev";
string[]output = null;
using (var ctx = new PrincipalContext(ContextType.Domain))
using (var user = UserPrincipal.FindByIdentity(ctx, username))
{
if (user != null)
{
output = user.GetGroups() //this returns a collection of principal objects
.Select(x => x.SamAccountName) // select the name. you may change this to choose the display name or whatever you want
.ToArray(); // convert to string array
}
}
return View(output);
}
查看
@model IEnumerable<SSSFT.LogIT.Models.ActiveDirectory>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.SamAccountName)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.SamAccountName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Details", "Details", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}
</table>
【问题讨论】:
标签: asp.net-mvc model-view-controller view active-directory-group