I have the following controller which displays a partial ListView. I am trying to display a list of the People Exposed for each control measure inside a list of control measures.

However when I run it I get the following error:

'System.Collections.Generic.IEnumerable<RACentral.ViewModels.FullControlMeasureListViewModel>' does not contain a definition for 'PersonsExpList' and no extension method 'PersonsExpList' accepting a first argument of type 'System.Collections.Generic.IEnumerable<RACentral.ViewModels.FullControlMeasureListViewModel>' could be found (are you missing a using directive or an assembly reference?)

Line 49:                 </td>
Line 50:                 <td>
Line 51:                     @for (int i = 0; i < Model.PeronsExpList.Count();   i++) 
Line 52:                     { 
Line 53:                     @Html.HiddenFor(x => Model.PeronsExpList[i].PeronExpId) 

Controller:

    public ActionResult FullControlMeasureList(int raId)
    {
        //Populate the list
        var hazards = new List<Hazard>(db.Hazards);
        var controlMeasures = new List<ControlMeasure>(db.ControlMeasures).Where(x => x.RiskAssessmentId == raId);

        var cmcombined = (
                              from g in hazards
                              join f in controlMeasures
                              on new { g.HazardId } equals new { f.HazardId }
                              select new CMCombined
                              {
                                  Activity = f.Activity,
                                  ControlMeasureId = f.ControlMeasureId,
                                  ExistingMeasure = f.ExistingMeasure,
                                  HazardName = g.Name,
                                  LikelihoodId = f.LikelihoodId,
                                  Rating = f.Rating,
                                  RiskAssessmentId = f.RiskAssessmentId,
                                  SeverityId = f.SeverityId,
                                  FurtherMeasure = f.FurtherRating >= 1 ? true : false,
                                  FurtherLikelihoodId = f.FurtherLikelihoodId,
                                  FurtherMeasureText = f.FurtherMeasure,
                                  FurtherRating = f.FurtherRating,
                                  FurtherSeverityId = f.FurtherSeverityId,
                              }).OrderBy(x => x.Activity).ToList();

        var cmPeopleExp = new List<ControlMeasurePeopleExposed>(db.ControlMeasurePeopleExposeds).Where(x => x.RiskAssessmentId == raId);
        var peopleExp = from c in cmPeopleExp
                        join d in db.PeopleExposeds
                        on c.PeopleExposedId equals d.PeopleExposedId
                        orderby d.Name
                        select new RAPeopleExp
                        {
                            RAPeopleExpId = c.PeopleExposedId,
                            PeopleExpId = c.PeopleExposedId,
                            PeopleExpName = d.Name,
                            RiskAssessmentId = c.RiskAssessmentId
                        };

        var model = cmcombined.Select(t => new FullControlMeasureListViewModel
        {
            ControlMeasureId = t.ControlMeasureId,
            HazardName = t.HazardName,
            LikelihoodId = t.LikelihoodId,
            PeopleExposed = t.PeopleExposedName,
            Rating = t.Rating,
            SeverityId = t.SeverityId,
            Activity = t.Activity,
            ExCM = t.ExistingMeasure,
            FurCM = t.FurtherMeasureText,
            FurtherLikelihoodId = t.FurtherLikelihoodId,
            FurtherRating = t.FurtherRating,
            FurtherSeverityId = t.FurtherSeverityId,
            FurtherMeasure = t.FurtherMeasure,
            PersonsExpList =
                            peopleExp.Where(v => v.RiskAssessmentId == t.RiskAssessmentId).Select(
                            x => new PersonsExpListViewModel { PersonExpId = x.PeopleExpId, PeronName = x.PeopleExpName, selected = false }).ToArray()
        });
        return PartialView("_FullControlMeasureList", model);
    }

View Models:

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

View:

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

Related posts

Recent Viewed