0

我正在尝试在这样的视图中访问我的列表:

  List<xxxx.Models.Enrollment> newStudentEnrollment=ViewBag.studentrecord;

但我收到一条错误消息:无法将类型“对象”隐式转换为“System.Collections.Generic.List”。存在显式转换(您是否缺少演员表?)...我尝试使用 FirstOrDefault() 和 ToList() 就像我在这个论坛中看到的其他人一样,但对我都没有。还尝试从此处删除 ToList() 'ViewBag.studentrecord = newRecord.ToList();' 在控制器中

这是我在控制器中所做的......

   var StudentRecord = (from E in db.Enrollments
                             join S in db.Students.Where(r => r.StudentID == intStudentNumber)
                    on E.StudentID equals S.StudentID into JoinedStudent
                             from AllStudent in JoinedStudent.DefaultIfEmpty()

                             select new
                             {

                               CourseID=E.CourseID,
                               StudentID=E.StudentID,
                               Date=E.Date,
                               InstructorFullName=E.InstructorFullName,
                               classDays=E.classDays,
                               listOfDays=E.listOfDays,
                               listOfTeachers=E.listOfTeachers,
                                 IsStudentEnroll = AllStudent != null ? true : false
                             }).ToList();


        var newRecord = from r in StudentRecord where r.StudentID==67 select r;
        ViewBag.studentrecord = newRecord.ToList();

看到我的数据是 List 类型的,我想我可以这样做……</p>

 List<xxxx.Models.Enrollment> newStudentEnrollment=ViewBag.studentrecord;

我如何使用它进行演员表或以任何方式使其工作?

非常感谢您的帮助。

编辑:

所以我尝试了这个:

  List<xxxx.Models.Enrollment> newStudentEnrollment =  (List<xxxx.Models.Enrollment>)ViewBag.studentrecord;

无法将“System.Collections.Generic.List 1[<>f__AnonymousType97[System.Int32,System.Int32,System.String,System.String,System.String,System.String,System.Boolean]]”类型的对象转换为“System” .Collections.Generic.List`1[xxxx.Models.Enrollment]'。

也试过:

    List<xxxx.Models.Enrollment> newStudentEnrollment = ViewBag.studentrecord as List<xxxx.Models.Enrollment>;

但 newStudentEnrollment 为空。我认为这与加入条款有关。它结合了两个对象,所以我必须以某种方式创建一个列表类型的通用对象。只有我不知道如何......有什么想法吗?谢谢。

更新

我像这样将类型从加入更改为注册。var newEnrollment = from e in context.Enrollment where e.SomeId ==someNumber select e;

比在视图中有效: List newStudentEnrollment = (List)ViewBag.studentrecord;

4

2 回答 2

0

你需要把你ViewBag.studentrecordList<xxxx.Models.Enrollment>

 List<xxxx.Models.Enrollment> newStudentEnrollment = (List<xxxx.Models.Enrollment>)ViewBag.studentrecord;
于 2013-11-10T08:21:43.360 回答
0

用这个 :

List<xxxx.Models.Enrollment> newStudentEnrollment=ViewBag.studentrecord as List<xxxx.Models.Enrollment>;
于 2013-11-10T08:21:48.717 回答