簡體   English   中英

在 ListView asp.net mvc 中顯示項目列表

[英]Display List of items within ListView asp.net mvc

我有以下顯示部分 ListView 的控制器。 我試圖在控制措施列表中顯示每個控制措施的暴露人員列表。

但是,當我運行它時,出現以下錯誤:

“System.Collections.Generic.IEnumerable<RACentral.ViewModels.FullControlMeasureListViewModel>”不包含“PersonsExpList”的定義,也沒有接受“System.Collections.Generic.IEnumerable<RACentral.ViewModels”類型的第一個參數的擴展方法“PersonsExpList” .FullControlMeasureListViewModel>' 可以找到(您是否缺少 using 指令或程序集引用?)

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) 

控制器:

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

查看型號:

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

您視圖中的模型是IEnumerable<FullControlMeasureListViewModel> ,而不是IEnumerable<PersonsExpListViewModel> 你需要嵌套循環

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

但是,這根本不會綁定到您的模型,因為表單控件的name屬性與您的模型沒有關系。 html 將是

<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" ... />

您需要將視圖中的模型更改為IList<FullControlMeasureListViewModel>並使用嵌套for循環,或者更好地使用自定義EditorTemplate類型FullControlMeasureListViewModelPersonsExpListViewModel所以在視圖中

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) 

正如我從您的代碼中了解到的,您正在使用部分視圖來填充PersonsExpList但它未包含在您的主視圖模型中( @model IEnumerable<RACentral.ViewModels.FullControlMeasureListViewModel> )。

我不知道您將如何在您的視圖中使用PersonsExpList ,但最好的方法是使用PersonsExpList在您的模型中填充PersonsExpList並使用下拉列表將其連接到您的視圖模型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM