gpt4 book ai didi

c# - MVC : How do you give a viewmodel a list and correctly output it on . cshtml

转载 作者:行者123 更新时间:2023-11-30 23:29:21 27 4
gpt4 key购买 nike

我所做的是搜索 Activedirectory 用户,给定值为名称。然后我创建一个 View 模型,其中包含名称、电子邮件和描述值。然后我在索引上将其显示为 .cshtml。

问题在于我的方法,它只通过它找到的第一个用户发送-(如果我从多个 Andrews 中搜索 Andrew,它会找到所有但返回第一个。)

我想将它们添加到列表中,然后几乎做同样的事情,但当然在 .cshtml 上迭代列表并显示结果。

这是 HomeController.cs 代码 --

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);
}

要注意的是

foreach(var found in srch.FindAll())
{
//users.Add(found);
IndexViewModel returnmodel = new IndexViewModel(found);
return View(returnmodel);
}

这是 IndexViewModel.cs 的代码#

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; }
}

这是 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>

最佳答案

Andy 的回答是正确的,但如果您仍然遇到问题,可以尝试以下代码。

首先创建新的用户类来保存你想要显示的用户的信息:

public class User
{
public string Name { get; set; }
public string Email { get; set; }
public string Description { get; set; }
}

然后修改你的IndexViewModel利用这个新类(class):

public class IndexViewModel
{
public List<User> FoundUsers {get; set;}

public string Name {get; set;}

public IndexViewModel(List<User> found)
{
this.FoundUsers = found;
}
}

在您的 Controller 中,您在识别问题区域方面是正确的,这将是解决问题的一种方法:

public ActionResult Index(IndexViewModel profile)
{
if (ModelState.IsValid)
{
List<User> users = new List<User>();
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(new User() {
Name = found.Name,
Email = found.Email,
Description = found.Description
});
}
}

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

return View(profile);
}

所以在 Controller 中,您现在正在用 List<User> 填充您的 View 模型你可以在你的 View 中迭代:

<table id="historyTable" class="table">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Description</th>
</tr>
</thead>
<tbody>
@foreach(var user in Model.FoundUsers)
{
<tr>
<td>@user.Name</td>
<td>@user.Email</td>
<td>@user.Description</td>
</tr>
}
</tbody>
</table>

关于c# - MVC : How do you give a viewmodel a list and correctly output it on . cshtml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35507008/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com