2016-02-19 45 views
1

我有充分的工作代码来搜索活动目录并使用MVC .cshtml显示它但我一直试图找出将所有找到的用户添加到列表中然后显示它们。目前它只显示找到的第一个用户。MVC AD搜索 - 以.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> 

EDIT ----------- 这是一个方法我试图 - --------------

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:\用户\ HGA \文档\ Visual Studio的2015年\项目\ AD内搜索的人\ AD内搜索的人\查看\首页\ Index.cshtml线:39

+0

的可能的复制[MVC:你如何给一个视图模型的列表,并正确地输出它.cshtml(http://stackoverflow.com/questions/35507008/mvc-怎么办,你放弃的一个 - 视图模型-A-列表和正确输出,它-ON-CSHTML) – MrDeveloper

回答

0

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

试试这个:

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

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