MVC AD搜索 - 以.cshtml显示多个结果

时间:2016-02-19 12:03:31

标签: asp.net-mvc list razor active-directory

我有完全正常工作的代码,搜索活动目录并使用MVC .cshtml显示它但我一直试图找出将所有找到的用户添加到列表然后显示它们。目前它只显示找到的第一个用户。

这是获取值的HomeController,搜索AD并返回结果。

    public class HomeController : Controller
    {
        public ActionResult Index(IndexViewModel profile)
        {
            if (ModelState.IsValid)
            {
                //List<Principal> users = new List<Principal>();
                using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
                {
                    UserPrincipal qbeUser = new UserPrincipal(ctx);
                    qbeUser.DisplayName = profile.Name + "*";

                    using (PrincipalSearcher srch = new PrincipalSearcher(qbeUser))
                    {
                        if(!(srch.FindAll().Count() < 0))
                        {
                            foreach(var found in srch.FindAll())
                            {
                                //users.Add(found);
                                IndexViewModel returnmodel = new IndexViewModel(found);

                                return View(returnmodel);
                            }
                        }                       
                    }
                }                               
            }
            return View(profile);
        }
   }

IndexViewModel

public class IndexViewModel
    {

        public IndexViewModel(Principal found)
        {
            Name = found.DisplayName;
            Email = found.UserPrincipalName;
            Description = found.Description;

        }

        [Required(ErrorMessage = "Please enter a name")]
        [Display(Name = "Persons Name")]
        public string Name { get; set; }
        public string Email { get; set; }
        public string Description { get; set; }
        //public List<Principal> user { get; set; }

    }

Index.cshtml

    <div id="content">
    @Html.ValidationSummary(true)
    @using (Html.BeginForm("Index", "Home"))
    {


        <fieldset>

            <div class="form-group col-md-12">
                @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
                <div class="col-md-4">
                    @Html.EditorFor(modelItem => Model.Name, new { htmlAttributes = new { @class = "form-control", @style = "width:280px" }, })
                    @Html.ValidationMessageFor(x => x.Name)
                </div>
                <div class="col-md-2">
                    <input type="submit" class="btn btn-default" value="Search">
                </div>
                <div class="col-md-3">

                </div>

            </div>
        </fieldset>
    }
    <br>
</div>
    <table id="historyTable" class="table">
     <thead>
        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>@Model.Name</td>
            <td>@Model.Email</td>
            <td>@Model.Description</td>
            </tr>
    </tbody>
</table>

修改----------- 这是我试过的一种方法----------------

HomeController.cs

public class HomeController : Controller
{
    public ActionResult Index(IndexViewModel profile)
    {
        if (ModelState.IsValid)
        {
            List<Principal> users = new List<Principal>();
            using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
            {
                UserPrincipal qbeUser = new UserPrincipal(ctx);
                qbeUser.DisplayName = profile.Name + "*";

                using (PrincipalSearcher srch = new PrincipalSearcher(qbeUser))
                {
                    if(!(srch.FindAll().Count() < 0))
                    {
                        foreach(var found in srch.FindAll())
                        {
                            users.Add(found);
                            IndexViewModel returnmodel = new IndexViewModel(users);
                            return View(returnmodel);
                        }
                    }                       
                }
            }                               
        }
        return View(profile);
    }

IndexViewModel.cs

public class IndexViewModel
{

    public IndexViewModel(List<Principal> found)
    {
        user = found;
    }

    [Required(ErrorMessage = "Please enter a name")]
    [Display(Name = "Persons Name")]
    public string Name { get; set; }
    public string Email { get; set; }
    public string Description { get; set; }
    public List<Principal> user { get; set; }

}

的index.html

<div id="content">
    @Html.ValidationSummary(true)
    @using (Html.BeginForm("Index", "Home"))
    {


        <fieldset>

            <div class="form-group col-md-12">
                @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
                <div class="col-md-4">
                    @Html.EditorFor(modelItem => Model.Name, new { htmlAttributes = new { @class = "form-control", @style = "width:280px" }, })
                    @Html.ValidationMessageFor(x => x.Name)
                </div>
                <div class="col-md-2">
                    <input type="submit" class="btn btn-default" value="Search">
                </div>
                <div class="col-md-3">

                </div>

            </div>
        </fieldset>
    }
    <br>
</div>
    <table id="historyTable" class="table">
     <thead>
        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        @using System.DirectoryServices.AccountManagement
        @foreach (Principal prin in Model.user)
        {
            <tr>
                <td>@prin.DisplayName</td>
                <td>@prin.UserPrincipalName</td>
                <td>@prin.Description</td>
             </tr>
        }        
    </tbody>
</table>

我在编译时遇到的错误是 -

对象引用未设置为对象的实例。   描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。

来源错误:

Line 37:     <tbody>
Line 38:         @using System.DirectoryServices.AccountManagement
Line 39:         @foreach (Principal prin in Model.user)
Line 40:         {
Line 41:             <tr>

源文件:C:\ Users \ hga \ Documents \ Visual Studio 2015 \ Projects \ Intra AD people searcher \ Intra AD people searcher \ Views \ Home \ Index.cshtml Line:39

2 个答案:

答案 0 :(得分:1)

您可以添加 if 语句来检查是否为空

@if(Model.user !=null)
{
   @foreach (Principal prin in Model.user)
     {
         <!--your code here-->
     }
}


答案 1 :(得分:0)

在你的控制器中,你的return语句在你的foreach循环中。所以第一次进入循环时,它会返回。这意味着你只有一个结果。

试试这个:

foreach(var found in srch.FindAll())
{
   users.Add(found);
}

IndexViewModel returnmodel = new IndexViewModel(users);
return View(returnmodel);