填充IEnumerable<;类别>;使用JSON结果
我以前在AdminController中有以下代码行,它成功地从课程中返回了相关小节的列表:填充IEnumerable<;类别>;使用JSON结果,json,linq,entity-framework,asp.net-mvc-4,Json,Linq,Entity Framework,Asp.net Mvc 4,我以前在AdminController中有以下代码行,它成功地从课程中返回了相关小节的列表: [AcceptVerbs(HttpVerbs.Get)] public JsonResult GetCourseSections(int courseID) { var Sections = dbcontext.CourseSection.Where(cs => cs.CourseID.Equals(courseID)).Select(x => new {
[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;set;},我正试图用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();
}
public void GetCourseSectionDetails(int courseID)
{
this.CourseSectionList = dbcontext.CourseSection.AsEnumerable().Where(cs => cs.CourseID.Equals(courseID)).Select(x => new CourseSection
{
CourseSectionID = x.CourseSectionID,
Title = x.Title
}).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);
}
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetCourseSections(int courseID)
{
var sections = avm.CourseSectionList;
return Json(sections, JsonRequestBehavior.AllowGet);
}
我收到错误无法在LINQ to Entities查询中构造实体或复杂类型“MetaLearning.Data.CourseSection”。如何使用节填充此.CourseSectionList变量?您不需要在控制器中重复相同的代码,而是直接将列表传递给视图 尽管如此,我还是要告诉您,在视图模型中放置数据访问代码比将其保存在控制器中更糟糕。我建议您使用特定的DAL层:
public interface IRepository
{
public IList<CourseSection> GetSections(int courseID);
}
正如错误消息所指出的,您不能在
linq to entities
中使用
.Select(m => new <Entity>{bla bla})
你可以找到一些很好的解释,解释为什么这是不可能的
编辑:
如果只想检索实体的某些属性,而不想使用DTO,也可以这样做:
return ctx
.CourseSection
.Where(cs => cs.CourseID.Equals(courseID))
//use an anonymous object to retrieve only the wanted properties
.Select(x => new
{
c= x.CourseSectionID,
t= x.Title,
})
//enumerate, good bye linq2entities
.ToList()
//welcome to linq2objects
.Select(m => new CourseSection {
CourseSectionID = m.c,
Title = m.t,
})
.ToList();
我以达林的回答为指导,做了如下工作: 视图模型
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();
}
public void GetCourseSectionDetails(int courseID)
{
this.CourseSectionList = dbcontext.CourseSection.AsEnumerable().Where(cs => cs.CourseID.Equals(courseID)).Select(x => new CourseSection
{
CourseSectionID = x.CourseSectionID,
Title = x.Title
}).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);
}
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetCourseSections(int courseID)
{
var sections = avm.CourseSectionList;
return Json(sections, JsonRequestBehavior.AllowGet);
}
很好的重构建议。那么这就行不通了?抱歉,我没有机会在我的开发机器上试用