MVC:如何给视图模型一个列表并在 .cshtml 上正确输出它

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

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

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

我想将它们添加到列表中,然后几乎做同样的事情,但当然在 .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>

我想你首先需要一个用户模型。然后让你的 IndexViewModel 有一个 List<User>。在您的 foreach 中,创建一个新用户 class 并将其添加到您的列表中。在 foreach 外部创建 IndexViewModel 并在 foreach 之后创建 return 它。然后循环浏览视图中的列表。这很常见,如果你 google 你会找到例子。

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

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

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

在您的控制器中,您对问题区域的识别是正确的,这是解决问题的一种方法:

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

因此,在控制器中,您现在使用可以在视图中迭代的 List<User> 填充视图模型:

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