次のようにビューでリストにアクセスしようとしています:
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__AnonymousType9
7[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 は null でした。join 句に関係していると思います。2 つのオブジェクトを組み合わせたので、どういうわけかリスト型のジェネリック オブジェクトを作成する必要があります。方法がわからないだけです...何かアイデアはありますか?ありがとう。
アップデート
このようにタイプを Join から Enrollment に変更しました。var newEnrollment = from e in context.Enrollment where e.SomeId == someNumber select e;
これが機能したビューよりも: List newStudentEnrollment = (List)ViewBag.studentrecord;