将Viewmodel对象发送到ASP .Net MVC中的剃刀视图

时间:2016-02-23 20:40:23

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor

在我的MVC项目中,我有一个学生模型类和一个部门模型类。为了建立一对多关系,我在学生模型类中有一个外键 DepartmentID。

    public class Student
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int StudentId { get; set; }
    public String LastName { get; set; }
    public String FirstName { get; set; }
    public String UserName { get; set; }
    public String Password { get; set; }

    [ForeignKey("Department")]
    public int DepartmentID { get; set; }

}

    public class Department
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int DepartmentID { get; set; }

    [Required(ErrorMessage="Department Name cannot be empty")]
    public String DepartmentName { get; set; }

    [Required(ErrorMessage = "Department Code cannot be empty")]
    public String DepartmentCode { get; set; }

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

但是我不想在Student Class中使用DepartmentID,而是希望用户看到DepartmentName。所以StudentViewModel类如下:

    //View Model
public class StudentViewModel
{
    public Student Student { get; set; }

    public int StudentId { get; set; }

    [Display(Name="StudentName")]
    [Required(ErrorMessage="Student Name cannot be left blank")]
    public String StudentFullName
    {
        get
        {
            return String.Format("{0} {1}", Student.FirstName, Student.LastName);
        }
    }

    public String UserName { get; set; }

    [DataType(DataType.Password)]
    public String Password { get; set; }

    [DataType(DataType.Password)]
    [Compare("Password",ErrorMessage="Passwords do not match")]
    [Display(Name="Confirm Password")]
    public String C_Password { get; set; }

    [Display(Name = "Department Name")]
    public String DepartmentName { get; set; }

}

但是如何在我的控制器操作方法中获取部门名称,该方法显示数据库中所有学生的列表。

    public ActionResult MyAction()
    {
        StudentContext context = new StudentContext();
        var students = context.Students.ToList();
        var departments = context.Departments.ToList();
        var student_dept_lst = new List<StudentViewModel>();
        var departmentName = String.Empty;

        foreach(var s in students)
        {
            var student_dept = new StudentViewModel();
            student_dept.DepartmentName =  from d in departments where d.DepartmentID == s.DepartmentID select d.DepartmentName;
        }

        return View(student_dept_lst ); 
    }

如何将DepartmentName链接到我从Context获得的列表中的每个学生对象?我知道这是微不足道的。但请帮助我。提前谢谢。

2 个答案:

答案 0 :(得分:3)

正确的做法是将Departement导航属性添加到Student类中,如下所示:

public class Student
{
    // Your properties you have already created still reamin here

    public Department Department { get; set; }
}

然后像这样对你的行动进行一些重构:

StudentContext context = new StudentContext();
var viewModels = context.Students
    .Include(s => s.Department) // tell EF to eager load the related department for each student
    .Select(s => new StudentViewModel // this make a projection to your view model
    {
        Student = s,
        DepartmentName = s.Department.DepartmentName
        // Set all properties you need
    }).ToList();

return View(viewModels); 

答案 1 :(得分:0)

看起来没问题,但是你没有将student_dept添加到studentviewmodel列表中。

此外,您应该选择一个选项 - 如果您在视图模型上有学生,则您不需要学生的每个属性(用户名,密码等)。您可以通过Student对象进行引用。或者在视图模型上保留这些属性并摆脱学生。然后,在视图模型上相应地设置属性。

foreach(var s in students)
{
    var student_dept = new StudentViewModel();
    student_dept.DepartmentName =  from d in departments where d.DepartmentID == s.DepartmentID select d.DepartmentName;
    student_dept.Student = s;
    //set other properties accordingly
    student_dept_lst.Add(student_dept);
}