无法计算如何从View中的GroupBy循环中获取标头数据

时间:2014-03-10 20:15:25

标签: c# asp.net-mvc-4

我迷失了正确的语法,以便在Foreach分组循环中显示特定的模型项。从控制器中提取IEnumerable Model ..子循环和外循环完美地工作,只是无法获取内部到头部的值并定义每个内部循环值的顶部

查看代码:

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

1 个答案:

答案 0 :(得分:0)

@ Html.DisplayFor(g =&gt; group.Key)

非常适合标题为分组元素的数据中的一个项目