我有一个viewmodel:
public class CourseViewModel
{
public int CourseID { get; set; }
public string Title { get; set; }
public string page_title { get; set; }
public string titleabbrev { get; set; }
public string Meta { get; set; }
public string intro_1 { get; set; }
public string intro_2 { get; set; }
public string intro_3 { get; set; }
public IEnumerable<course_section> Course_section { get; set; }
public IEnumerable<course_subsection> Course_subsection { get; set; }
public Enrollment Enrollments { get; set; }
}
在我的控制器中,我有以下内容:
var allcourse = db.Course_page
.Where(i => i.titleabbrev == courseabbrev)
.Include(i => i.course_sections.Select(c => c.course_subsections))
.Include(i => i.enrollments);
var viewModel = new CourseViewModel();
viewModel.CourseID = allcourse.Single().CourseID;
viewModel.page_title = allcourse.Single().page_title;
viewModel.titleabbrev = allcourse.Single().titleabbrev;
viewModel.Title = allcourse.Single().Title;
viewModel.intro_1 = allcourse.Single().intro_1;
viewModel.intro_2 = allcourse.Single().intro_2;
viewModel.intro_3 = allcourse.Single().intro_3;
viewModel.Course_section = allcourse
.Single().course_sections
.OrderBy(i => i.Order);
viewModel.Course_subsection = viewModel.Course_section
.SelectMany(c => c.course_subsections)
.Where(i => i.Titleabbrev == sectionabbrev)
.OrderBy(i => i.Order);
return View(viewModel);
我想发送到视图viewmodel.enrollments,但是我在执行此操作时遇到问题,因为我只想将单个注册记录发送到viewmodel中的视图,并且在course_page模型中注册是IEnumerable。
据推测,当我创作“allcourse”时,我应该只采用单一的注册记录,但无论我尝试什么,我似乎都无法做到这一点然后将其返回到视图。我曾尝试过例如:
var allcourse = db.Course_page
.Where(i => i.titleabbrev == courseabbrev)
.Where(b => b.enrollments.Single().UserID == User.Identity.GetUserId())
.Include(i => i.course_sections.Select(c => c.course_subsections))
.Include(i => i.enrollments);
将viewmodel发送到视图的最佳方法是什么,只有与当前登录的userID匹配的注册记录?
编辑 - 我也尝试过这样的事情:
var allcourse = db.Course_page
.Where(i => i.titleabbrev == courseabbrev)
Where(i => i.Enrollments.Any(b => b.UserID == User.Identity.GetUserId())
.Include(i => i.course_sections.Select(c => c.course_subsections))
.Include(i => i.enrollments);
在linqpad中测试此项会返回UserID不匹配的注册记录。
由于
答案 0 :(得分:1)
我建议您将起点更改为注册,然后从课程页面和其他链接实体中获取值。
e.g。
$(document).ready(function () {
var table = $('#calendarView').DataTable({
});
var pages = table.page.info().length;
for (i = 0; i <= pages; i++) {
table.page(i).draw();
if(i===pages){
i=0;
}
}
});
答案 1 :(得分:1)
尝试此查询:
var userId = User.Identity.GetUserId();
var viewModel = db.Course_page
.Where(i => i.titleabbrev == courseabbrev)
.Where(i => i.Enrollments.Any(b => b.UserID == userId))
.Select(e => new CourseViewModel{
CourseID = e.CourseID,
page_title = e.page_title,
titleabbrev = e.titleabbrev,
Title = e.Title,
intro_1 = e.intro_1,
intro_2 = e.intro_2,
intro_3 = e.intro_3,
Course_section = e.course_sections.OrderBy(i => i.Order)
.ToList(),
Course_subsection = e.course_sections
.SelectMany(c => c.course_subsections)
.Where(i => i.Titleabbrev == sectionabbrev)
.OrderBy(i => i.Order).ToList()
}).Single();