我以前有下面的代码行从我AdminController已成功从一个疗程内恢复相关小节的列表中:填充IEnumberable <class>用JSON结果
[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();
}
AdminController
[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);
}
我收到错误实体或复杂类型 'MetaLearning.Data.CourseSection' 不能在LINQ to Entities查询中构造。如何使用节填充this.CourseSectionList变量?
我不认为解决他们虽然错误。 – asymptoticFault
虽然好的重构建议。 – asymptoticFault
但是你不能投影模型的实体... –