【问题标题】:Display Active Directory Group in a MVC view在 MVC 视图中显示 Active Directory 组
【发布时间】: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


    【解决方案1】:

    你可以通过简单地更新你的@model来修复这个错误:

    @model String[]
    

    您当前正在将 String[] 传递给您的视图,同时期望 IEnumerable 为 SSSFT.LogIT.Models.ActiveDirectory。更新您的代码以返回正确类型的数据,或者根据您返回的实际结果调整您的强类型视图。

    【讨论】:

    • Sakuto 感谢您的回复,请问如何更改代码以返回正确的数据类型。我试图在不起作用的视图中将 IEnumerable 更改为 String[]
    猜你喜欢
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    相关资源
    最近更新 更多