我有充分的工作代码来搜索活动目录并使用MVC .cshtml显示它但我一直试图找出将所有找到的用户添加到列表中然后显示它们。目前它只显示找到的第一个用户。MVC AD搜索 - 以.cshtml的形式显示多个结果
这是HomeController接受一个值,搜索AD并返回结果。
public class HomeController : Controller
{
public ActionResult Index(IndexViewModel profile)
{
if (ModelState.IsValid)
{
//List users = new List();
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 user { get; set; }
}
Index.cshtml
@Html.ValidationSummary(true)
@using (Html.BeginForm("Index", "Home"))
{
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
@Html.EditorFor(modelItem => Model.Name, new { htmlAttributes = new { @class = "form-control", @style = "width:280px" }, })
@Html.ValidationMessageFor(x => x.Name)
}
NameEmailDescription
@Model.Name@Model.Email@Model.DescriptionEDIT ----------- 这是一个方法我试图 - --------------
个HomeController.cs
public class HomeController : Controller
{
public ActionResult Index(IndexViewModel profile)
{
if (ModelState.IsValid)
{
List users = new List();
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 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 user { get; set; }
}
的index.html
@Html.ValidationSummary(true)
@using (Html.BeginForm("Index", "Home"))
{
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
@Html.EditorFor(modelItem => Model.Name, new { htmlAttributes = new { @class = "form-control", @style = "width:280px" }, })
@Html.ValidationMessageFor(x => x.Name)
}
NameEmailDescription
@using System.DirectoryServices.AccountManagement
@foreach (Principal prin in Model.user)
{
@prin.DisplayName@prin.UserPrincipalName@prin.Description}
我得到的编译错误是 -
未将对象引用设置为对象的实例。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。
异常详细信息:System.NullReferenceException:对象不设置为一个对象的一个实例。
源错误:
Line 37:
Line 38: @using System.DirectoryServices.AccountManagement
Line 39: @foreach (Principal prin in Model.user)
Line 40: {
Line 41:
源文件:C:\用户\ HGA \文档\ Visual Studio的2015年\项目\ AD内搜索的人\ AD内搜索的人\查看\首页\ Index.cshtml线:39
2016-02-19
hayhay
+0
的可能的复制[MVC:你如何给一个视图模型的列表,并正确地输出它.cshtml(http://stackoverflow.com/questions/35507008/mvc-怎么办,你放弃的一个 - 视图模型-A-列表和正确输出,它-ON-CSHTML) –