【问题标题】:Passing grouped linq object to view将分组的 linq 对象传递给查看
【发布时间】:2013-02-19 20:53:47
【问题描述】:

我试图将 linq 列表对象从我的控制器传递到我的视图。 linq 对象包含一个引发某种错误的分组。我只想在视图中显示分组的对象。 linq 语句完美运行,但显示语句却不行!任何帮助将不胜感激!

控制器

        public ViewResult StudentAttendanceForYear(int id)
    {

        DateTime finDate = System.DateTime.Today;
        DateTime strtDate = DateTime.Today.AddMonths(-6);


        var chosenStudent = (from t in db.ClassInstanceDetails.Include("Student")
                                 where (t.Attendance == false) && (t.StudentID == id)
                                 && (t.ClassInstance.Date > strtDate) && (t.ClassInstance.Date < finDate)
                                 group t by new { t.ClassInstance.Date.Year, t.ClassInstance.Date.Month, t.ClassInstance.Date.Day } into grp
                                 select new
                                 {

                                     absentDate = grp.Key,
                                     numAbsences = grp.Count(t => t.Attendance == false)

                                 }).ToList();



        return View(chosenStudent.ToList());
    }

查看

我尝试将视图更改为

@model IEnumerable<System.Linq.IGrouping<object, FYPSchoolApp.DAL.ClassInstanceDetail>>

但仍然没有运气,我不断收到以下错误:

传入字典的模型项的类型为'System.Collections.Generic.List1[&lt;&gt;f__AnonymousType72[f__AnonymousType63[System.Int32,System.Int32,System.Int32],System.Int32]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[System.Linq.IGrouping`2[System.Object,FYPSchoolApp.DAL。 ClassInstanceDetail]]'。

【问题讨论】:

    标签: asp.net-mvc linq razor


    【解决方案1】:

    不要尝试将匿名类型作为模型传递到视图中。

    你需要的是一个 ViewModel:

    public class AbsentCountViewModel
    {
       public DateTime absentDate { get; set; }
       public int numAbsences { get; set; }
    }
    

    然后更改您的查询以选择到您的视图模型中

    var chosenStudent = 
       (from t in ...
       group t by new 
       { 
               t.ClassInstance.Date.Year, 
               t.ClassInstance.Date.Month, 
               t.ClassInstance.Date.Day 
       } into grp
       select new
       {
           absentDate = grp.Key,
           numAbsences = grp.Count(t => t.Attendance == false)
       }).ToList()
       // you need to do the select in two steps 
       // because EF cannot translate the new DateTime
       .Select(item => new AbsenctCountViewModel
       {
           absentDate = new DateTime(item.absentDate.Year, 
                                     item.absentDate.Month, 
                                     item.absentDate.Day)
           numAbsences = item.numAbsences
       }).ToList();
    
    return View(chosenStudent);
    

    最后,您可以使用@model 访问视图中的结果:

    @model List<AbsenctCountViewModel>
    

    【讨论】:

    • 非常感谢您的回复,我对这一切还比较陌生!我已经创建了 VIewModel 并输入了您的代码,但现在我收到此错误“LINQ to Entities 仅支持无参数构造函数和初始化程序”。关于如何解决它的任何想法?
    • 对不起,这是由new DateTime(... 引起的,有多种解决方法...我会更新我的答案。
    • 解决了!非常非常感谢!可以肯定地说,我永远不会自己解决这个问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    相关资源
    最近更新 更多