我有以下控制器显示部分ListView。我试图在控制措施列表中显示每个控制措施的人员列表。
然而,当我运行它时,我收到以下错误:
'System.Collections.Generic.IEnumerable< RACentral.ViewModels.FullControlMeasureListViewModel>'不包含'PersonsExpList'的定义,也没有扩展方法'PersonsExpList'接受类型'System.Collections.Generic.IEnumerable< RACentral.ViewModels.FullControlMeasureListViewModel>'的第一个参数可以找到(你错过了使用指令或程序集引用吗?)
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>
}
}
答案 0 :(得分:1)
视图中的模型为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
进行类型FullControlMeasureListViewModel
和{{1所以在视图中
PersonsExpListViewModel
答案 1 :(得分:0)
我从您的代码中了解到,您使用部分视图来填充PersonsExpList
,但它未包含在主视图中的模型中(@model IEnumerable<RACentral.ViewModels.FullControlMeasureListViewModel>
)。
我不知道您在视图中如何使用PersonsExpList
,但最好的方法是使用viewbag在模型中填充PersonsExpList
并使用下拉列表将其连接到您的视图模型。