Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC:如何为viewmodel提供一个列表并将其正确输出到.cshtml上_C#_Asp.net Mvc_Asp.net Mvc 4_Razor_Active Directory - Fatal编程技术网

C# MVC:如何为viewmodel提供一个列表并将其正确输出到.cshtml上

C# MVC:如何为viewmodel提供一个列表并将其正确输出到.cshtml上,c#,asp.net-mvc,asp.net-mvc-4,razor,active-directory,C#,Asp.net Mvc,Asp.net Mvc 4,Razor,Active Directory,我所做的是用给定的值作为名称搜索Activedirectory用户。然后,我创建一个viewmodel,其中包含值Name、Email和description。然后在索引中将其显示为.cshtml 问题在于我的制作方式,它只通过它找到的第一个用户发送信息(如果我从多个Andrew中搜索Andrew,它会找到所有的Andrew,但会返回第一个。) 我想将它们添加到一个列表中,然后做几乎相同的事情,但是当然在.cshtml上迭代列表并显示结果 这是HomeController.cs代码-- 以下是I

我所做的是用给定的值作为名称搜索Activedirectory用户。然后,我创建一个viewmodel,其中包含值Name、Email和description。然后在索引中将其显示为.cshtml

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

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

这是HomeController.cs代码--

以下是IndexViewModel.cs的代码#

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

@Html.ValidationSummary(true)
@使用(Html.BeginForm(“索引”、“主页”))
{
@LabelFor(model=>model.Name,新的{@class=“controllabel col-md-2”})
@EditorFor(modeleItem=>Model.Name,new{htmlAttributes=new{@class=“form control”,@style=“width:280px”},})
@Html.ValidationMessageFor(x=>x.Name)
}

此处显示找到的单个结果/

<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有一个
列表
。在foreach中,创建一个新的用户类并将其添加到列表中。在foreach外部创建IndexViewModel,并在foreach之后返回它。然后在视图中循环浏览列表。这是很常见的,如果你在谷歌上搜索,你会发现一些例子

Andy的答案是正确的,但如果您仍然有问题,您可能希望尝试以下代码

首先创建新的用户类以保存有关要显示的用户的信息:

public class User
{
  public string Name { get; set; }
  public string Email { get; set; }
  public string Description { get; set; }
}
然后修改您的
索引模型
,以使用此新类:

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

  public string Name {get; set;}

  public IndexViewModel(List<User> found)
  {
    this.FoundUsers = found;
  }
}
公共类索引模型
{
公共列表FoundUsers{get;set;}
公共字符串名称{get;set;}
公共索引模型(找到列表)
{
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);
}
public ActionResult索引(索引模型配置文件)
{
if(ModelState.IsValid)
{
列表用户=新列表();
使用(PrincipalContext ctx=新PrincipalContext(ContextType.Domain))
{
UserPrincipal qbeUser=新的UserPrincipal(ctx);
qbeUser.DisplayName=profile.Name+“*”;
使用(PrincipalSearcher srch=新PrincipalSearcher(qbeUser))
{
如果(!(srch.FindAll().Count()<0))
{
foreach(在srch.FindAll()中找到的变量)
{
添加(新用户(){
Name=found.Name,
Email=found.Email,
Description=找到。Description
});
}
}                       
IndexViewModel returnmodel=新IndexViewModel(用户);
返回视图(返回模型);
}
}
}
返回视图(剖面);
}
因此,在控制器中,您现在使用一个
列表填充视图模型,您可以在视图中迭代该列表:

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

名称
电子邮件
描述
@foreach(Model.FoundUsers中的var用户)
{
@用户名
@用户邮箱
@用户描述
}

我只是快速查看了一下,但是循环中的return语句只会产生一个结果。您应该将foreach放在视图中:@foreach(var..in…{@Model.prop}看起来您是在将所有找到的用户放入列表之前从控制器返回视图。尝试将您的
返回视图(returnModel)
移动到您的
foreach
循环之外我会尝试一下,我很难准确地说出您想说的话我确实发现很难找到正确的搜索方式,我已经寻找了一段时间。效果很好,必须调整一些内容,并在foreach中添加一个空检查。谢谢你的帮助!
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);
}
<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>