如何将IEnumerable对象的单个记录作为视图模型的一部分返回



我有一个视图模型:

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,但我在执行此操作时遇到问题,因为我只想向视图模型中的视图发送一条注册记录,而在课程页面模型中,注册是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);

将视图模型发送到只有与当前登录的用户ID匹配的注册记录的视图的最佳方式是什么?

编辑-我也尝试过类似的东西:

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不匹配的注册记录。

感谢

我建议您将起点更改为注册,然后从课程页面和其他链接实体中获取值。。

例如

  var allcourse = db.Enrollments
        .Where(e => e.UserID == User.Identity.GetUserId())
        .Select(x => new CourseViewModel { 
    CourseID = e.Course_page.CourseID,
    ...
    ...
    Course_section = e.Course_page.course_sections.OrderBy(i => i.Order).FirstOrDefault
    ...
    ...
    ...
        });

尝试此查询:

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();

相关内容

  • 没有找到相关文章

最新更新