私は以前、コース内から関連するサブセクションのリストを正常に返す、私の AdminController 内からの次のコード行を持っていました:
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetCourseSections(int courseID)
{
var Sections = dbcontext.CourseSection.Where(cs => cs.CourseID.Equals(courseID)).Select(x => new
{
sectionID = x.CourseSectionID,
sectionTitle = x.Title
);
return Json(Sections, JsonRequestBehavior.AllowGet);
}
dbcontext を呼び出すのは悪い習慣であるため、コントローラーからこれを取り出すように通知されたので、これを AdminViewModel に移動しました。私の AdminViewModel 内には、変数public List CourseSectionList { get; があります。設定; この変数に JSON リクエストの詳細を設定しようとしています。私のコードは次のとおりです。
AdminViewModel
public void GetCourseSectionDetails(int courseID)
{
var Sections = dbcontext.CourseSection.Where(cs => cs.CourseID.Equals(courseID)).Select(x => new CourseSection
{
CourseSectionID = x.CourseSectionID,
Title = x.Title
});
this.CourseSectionList = Sections.ToList();
}
管理者コントローラ
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetCourseSections(int courseID)
{
avm.GetCourseSectionDetails(courseID);
var Sections = avm.CourseSectionList.Where(cs => cs.CourseID.Equals(courseID)).Select(x => new
{
sectionID = x.CourseSectionID,
sectionTitle = x.Title
});
System.Diagnostics.EventLog.WriteEntry("Application", "JSON=" + Sections.ToList(), System.Diagnostics.EventLogEntryType.Error);
return Json(Sections, JsonRequestBehavior.AllowGet);
}
The entity or complex type 'MetaLearning.Data.CourseSection' cannot be construct in a LINQ to Entities query.というエラーが表示されます。セクションを使用して this.CourseSectionList 変数を設定するにはどうすればよいですか?