C# 在ListView asp.net mvc中显示项目列表

C# 在ListView asp.net mvc中显示项目列表,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我有以下显示部分ListView的控制器。我试图在控制措施列表中显示每个控制措施暴露的人员列表 但是,当我运行它时,会出现以下错误: “System.Collections.Generic.IEnumerable”不包含“PersonsExpList”的定义,并且找不到接受“System.Collections.Generic.IEnumerable”类型的第一个参数的扩展方法“PersonsExpList”(是否缺少using指令或程序集引用?) 视图: @model IEnumerable

我有以下显示部分ListView的控制器。我试图在控制措施列表中显示每个控制措施暴露的人员列表

但是,当我运行它时,会出现以下错误:

“System.Collections.Generic.IEnumerable”不包含“PersonsExpList”的定义,并且找不到接受“System.Collections.Generic.IEnumerable”类型的第一个参数的扩展方法“PersonsExpList”(是否缺少using指令或程序集引用?)

视图:

@model IEnumerable
@foreach(模型中的var组
.OrderBy(x=>x.HazardName)
.GroupBy(x=>x.HazardName))
{
@组键
@DisplayNameFor(model=>model.Activity)
@DisplayNameFor(model=>model.PeopleExposed)
@DisplayNameFor(model=>model.ExCM)
@DisplayNameFor(model=>model.SeverityId)
@DisplayNameFor(model=>model.LikelihoodId)
@DisplayNameFor(model=>model.Rating)
@DisplayNameFor(model=>model.FurCM)
@DisplayNameFor(model=>model.FutureServeryId)
@DisplayNameFor(model=>model.FutureLikeLikeLihoodId)
@Html.DisplayNameFor(model=>model.deferrating)
foreach(组中的var项目)
{
@Html.DisplayFor(modelItem=>item.Activity)@Html.HiddenFor(modelItem=>item.ControlMeasureId)
@对于(int i=0;iModel.personexplist[i].PersonExpId)
@Html.CheckBoxFor(x=>Model.personexplist[i]。选中)
@DisplayFor(x=>Model.personexplist[i].PersonName,Model.peronexplist[i].PeronName)
} 
@DisplayFor(modeleItem=>item.ExCM)
@DisplayFor(modelItem=>item.SeverityId)
@DisplayFor(modelItem=>item.LikelihoodId)
@DisplayFor(modelItem=>item.Rating)
@DisplayFor(modeleItem=>item.FurCM)
@DisplayFor(modelItem=>item.FurthSeverityId)
@DisplayFor(modelItem=>item.FutureLikeLikeLihoodId)
@DisplayFor(modelItem=>item.detainRating)
}
}

我从您的代码中了解到,您正在使用局部视图填充
PersonExplist
,但它未包含在主视图中的模型中(
@model IEnumerable


我不知道如何在视图中使用
personexplist
,但最好的方法是使用viewbag在模型中填充
personexplist
,并使用下拉列表将其连接到视图模型

您视图中的模型是
IEnumerable
,而不是
IEnumerable
。您需要嵌套循环

foreach (var item in Model)
{
  // item is FullControlMeasureListViewModel
  for (int i = 0; i < item.PeronsExpList.Count; i++) 
  { 
    @Html.HiddenFor(x => item.PeronsExpList[i].PeronExpId) 
但是需要

<input name="[0].PeronsExpList[0].PeronExpId" ... />
<input name="[0].PeronsExpList[1].PeronExpId" ... />
<input name="[1].PeronsExpList[0].PeronExpId" ... />

您的
cmcombined.Select
将返回一个
IEnumerable
,但是在您的部分中,您希望引用
peronexplist
,就像它是
FullControlMeasureListViewModel
的一个实例一样。谢谢@Jayme我如何正确引用它?
public class FullControlMeasureListViewModel
{
    public int ControlMeasureId { get; set; }

    [Display(Name = "People Exposed")]
    public string PeopleExposed { get; set; }

    [Display(Name = "Hazard")]
    public string HazardName { get; set; }

    [Display(Name = "S")]
    public int SeverityId { get; set; }

    [Display(Name = "L")]
    public int LikelihoodId { get; set; }

    [Display(Name = "R")]
    public int Rating { get; set; }

    [Display(Name = "Activity")]
    public string Activity { get; set; }

    [Display(Name = "Extisting Control Measure")]
    public string ExCM { get; set; }

    [Display(Name = "Further Control Measure")]
    public string FurCM { get; set; }

    [Display(Name = "S")]
    public int? FurtherSeverityId { get; set; }

    [Display(Name = "L")]
    public int? FurtherLikelihoodId { get; set; }

    [Display(Name = "R")]
    public int? FurtherRating { get; set; }

    public bool FurtherMeasure { get; set; }

    public PersonsExpListViewModel[] PersonsExpList { get; set; }
}

    public class PersonsExpListViewModel
{
    public int PersonExpId { get; set; }

    [Display(Name = "Person")]
    public string PeronName { get; set; }

    public bool selected { get; set; }
}
 @model IEnumerable<RACentral.ViewModels.FullControlMeasureListViewModel>
 <table class="table">
 @foreach (var group in Model
            .OrderBy(x => x.HazardName)
            .GroupBy(x => x.HazardName))
 {
    <tr class="group-header">
        <th colspan="12"><h4>@group.Key</h4></th>
    </tr>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Activity)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PeopleExposed)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ExCM)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SeverityId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LikelihoodId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Rating)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FurCM)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FurtherSeverityId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FurtherLikelihoodId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FurtherRating)
        </th>
    </tr>

    foreach (var item in group)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Activity) @Html.HiddenFor(modelItem => item.ControlMeasureId)
            </td>
            <td>
                @for (int i = 0; i < Model.PeronsExpList.Count(); i++) 
                { 
                @Html.HiddenFor(x => Model.PersonsExpList[i].PersonExpId)
                @Html.CheckBoxFor(x => Model.PersonsExpList[i].selected)
                @Html.DisplayFor(x => Model.PersonsExpList[i].PersonName, Model.PeronsExpList[i].PeronName)
                } 
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ExCM)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SeverityId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LikelihoodId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Rating)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FurCM)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FurtherSeverityId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FurtherLikelihoodId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FurtherRating)
            </td>
        </tr>
    }
}
foreach (var item in Model)
{
  // item is FullControlMeasureListViewModel
  for (int i = 0; i < item.PeronsExpList.Count; i++) 
  { 
    @Html.HiddenFor(x => item.PeronsExpList[i].PeronExpId) 
<input name="item.PeronsExpList[0].PeronExpId" ... />
<input name="item.PeronsExpList[1].PeronExpId" ... />
<input name="[0].PeronsExpList[0].PeronExpId" ... />
<input name="[0].PeronsExpList[1].PeronExpId" ... />
<input name="[1].PeronsExpList[0].PeronExpId" ... />
for(int i = 0; i < Model.Count; i++)
{
  for (int j = 0; i < Model[i].PeronsExpList.Count; i++) 
  { 
    @Html.HiddenFor(x => x[i].PeronsExpList[j].PeronExpId)