无法确定如何从视图中的GroupBy循环中获取标头数据
本文关键字:循环 获取 数据 GroupBy 视图 无法确定 | 更新日期: 2023-09-27 17:49:16
我迷失了正确的语法,无法获得特定的模型项目,仅在Foreach
分组循环中显示。从控制器的IEnumerable Model
馈电。子循环和外部循环工作得很好,只是不能获得内部to头的值并定义每个内部循环值的Top
视图代码:
@model IEnumerable<eManager.Web2.Models.EventClassCompListVM>
<table class="table">
<h2>
@(Model.Any() ? Model.First().EventName : "")
@(Model.Any() ? Model.First().EventDate.ToShortDateString() : "")
</h2>
@foreach (var group in Model.GroupBy(item => item.ClassName))
{
<tr>
<td>
@Html.DisplayNameFor(model => model.ClassName)
@Html.DisplayFor(model => model.ClassName) //Generates an unknown exception
</td>
</tr>
foreach (var item in group)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Comp_EventID)
</td>
<td>
@Html.DisplayFor(modelItem => item.CompName)
ViewModel//由特定元素的4个模型组成
namespace eManager.Web2.Models
{
public class EventClassCompListVM
{
public EventClassCompListVM()
{
this.ClassName = ClassName;
this.CompName = CompName;
}
public int ECCLkey { get; set; }
public int CompeditorId { get; set; }
public int Comp_EventID { get; set; }
public DateTime EventDate { get; set; }
public int EventID { get; set; }
public int ClassID { get; set; }
public string CompName { get; set; }
public string EventName { get; set; }
public string CompMetricName { get; set; }
public double CompMetric { get; set; }
public int MaxComp { get; set; }
public string ClassName { get; set; }
public int ClassOrder { get; set; }
public int EventClassID { get; set; }
public bool IsCrossOver { get; set; }
public int Event_Class_CompeditorsId {get; set; }
}
}
控制器public ActionResult BuildEventClassReports()
{
var model = (from o in _db.Events
join o2 in _db.Event_Classes on o.EventID equals o2.EventID
where o.EventID.Equals(o2.EventID)
join o3 in _db.Event_Class_Compeditors_s on o2.EventClassID equals o3.EventClassID
where o2.EventClassID.Equals(o3.EventClassID)
join o4 in _db.Compeditors on o3.CompeditorId equals o4.CompeditorId
where o3.CompeditorId.Equals(o4.CompeditorId)
join o5 in _db.Class_Definitions on o2.ClassID equals o5.Class_Definition_ID
where o2.ClassID.Equals(o5.Class_Definition_ID)
where o.CurrentEvent.Equals(true)
orderby o2.ClassOrder
//orderby o3.Comp_EventID
select new EventClassCompListVM
{
ClassID = o2.ClassID,
EventName = o.EventName,
EventDate = o.Date_End,
ClassName = o5.Class_Name,
Comp_EventID = o3.Comp_EventID,
ClassOrder = o2.ClassOrder,
CompName = (o4.FirstName + " " + o4.LastName),
IsCrossOver = o3.IsCrossOver
}).ToList();
return View(model);
}
@Html。DisplayFor(g => group.Key)
对于数据中标题为分组元素的一个项目非常有效