0

在我的应用程序中,我正在尝试编写一个 ListStudents ActionResult,它只显示一个学生列表,其ClassroomID等于当前登录的教师创建的 ClassroomID 之一。

我相信我需要使用一些 LINQ 查询来获得所需的结果,但我被卡住了,因为当教师实体有一个 ICollection 教室时,我不太知道如何处理它。

我的 TeacherController 中的 ListStudents 和 CreateClassroom

public ActionResult ListStudents()
{
        //This gets the current teachers UserProfile as only users with the teacher role have access to this controller

        var teacherProfile = db.UserProfiles.Local.SingleOrDefault(u => u.UserName == User.Identity.Name)
            ?? db.UserProfiles.SingleOrDefault(u => u.UserName == User.Identity.Name);

        var teacher = db.Teachers.Find(teacherProfile.UserId);

        //Need query where students ClassroomID needs to be one of the current teachers ClassroomID's

        return View(students);
}

public ActionResult CreateClassroom(Classroom model)
    {
        if (ModelState.IsValid)
        {
            try
            {
                var userProfile = db.UserProfiles.Local.SingleOrDefault(u => u.UserName == User.Identity.Name)
                    ?? db.UserProfiles.SingleOrDefault(u => u.UserName == User.Identity.Name);

                var classroom = new Classroom
                {
                    ClassroomName = model.ClassroomName,
                    TeacherID = userProfile.UserId

                };
                db.Classrooms.Add(classroom);
                db.SaveChanges();
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Something went wrong, try again.");
            }
        }
        return RedirectToAction("ListClassrooms");
    }    

用户、学生、教师和课堂模型

    public class User
{
    [Key]
    [Editable(false)]
    [ForeignKey("UserProfile")]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
    public int UserId { get; set; }

    public virtual UserProfile UserProfile { get; set; }

    [Display(Name = "First Name")]
    [Required(ErrorMessage = "First name is required.")]
    [MaxLength(50)]
    public virtual string FirstName { get; set; }

    [Display(Name = "Last Name")]
    [Required(ErrorMessage = "Last name is required.")]
    [MaxLength(50)]
    public virtual string LastName { get; set; }
}

学生

    public class Student : User
{
    [ForeignKey("Account")]
    [Editable(false)]
    public virtual int AccountID { get; set; }

    public virtual Account Account { get; set; }

    [Column("ClassroomID")]
    [ForeignKey("Classroom")]
    public virtual int ClassroomID { get; set; }

    public virtual Classroom Classroom { get; set; }
}

老师

    public class Teacher : User
{
    public virtual ICollection<Classroom> Classrooms { get; set; }
}

课堂

    public class Classroom
{
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public virtual int ClassroomID { get; set; }

    [Display(Name = "Classroom Name")]
    [MaxLength(50)]
    public virtual string ClassroomName { get; set; }

    public virtual ICollection<Student> Students { get; set; }

    [Column("TeacherID")]
    [ForeignKey("Teacher")]
    public virtual int TeacherID { get; set; }

    public virtual Teacher Teacher { get; set; }
}

感谢您抽出时间帮助我解决这个问题,我对 MVC 很陌生,并且正在努力学习。

4

1 回答 1

3

分成几行,这样更容易理解。

var classrooms = db.Classrooms.Where(x => x.TeacherID == teacher.Id).Select(x => x.ClassroomID);
var students = db.Students.Where(x => classrooms.Contains(x.ClassroomID);
于 2013-06-23T18:22:28.763 回答