html搜索显示结果,MVC AD搜索 - 以.cshtml的形式显示多个结果

我有充分的工作代码来搜索活动目录并使用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.Description

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

个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) –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值