在ASP .Net MVC中查看模型数据

时间:2016-02-09 18:54:06

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

在我的项目中有两个域模型类:Student.cs和Department.cs。学生班和部门班之间有多对一的关系。

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; }
    }

现在在View中显示Full Name而不是名称组件回到最终用户我想出了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);
        }
        set
        {
            if (StudentFullName.Length > 1)
            {
                string[] tokens = StudentFullName.Split(' ');
                Student.FirstName = tokens[0];
                Student.LastName = tokens[tokens.Length - 1];
            }
        }
    }

    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; }
}

在上下文数据库中,初始化程序类是我的种子方法:

  protected override void Seed(StudentContext context)
        {
            var departments = new List<Department>
            {
                new Department {DepartmentID=1,DepartmentName="CSE",DepartmentCode="CS101"},
                new Department {DepartmentID=2,DepartmentName="ECE",DepartmentCode="EC197"},
                new Department {DepartmentID=3,DepartmentName="MECH",DepartmentCode="MC202"}
            };

            departments.ForEach(d => context.Departments.Add(d));
            context.SaveChanges();

            var students = new List<StudentViewModel> {
                new StudentViewModel {StudentId=1,StudentFullName = "Zack Dyas",UserName ="zack_dyas",DepartmentID=1,Password ="zack123"},
                new StudentViewModel {StudentId=2,StudentFullName = "Abraham D'Pedro",UserName ="ab_dpedro",DepartmentID=2,Password ="ab123"}
            };

            students.ForEach(s => context.Students.Add(s));
            context.SaveChanges();
}

在视图中,我如何显示部门名称而不是2个学生记录的模型中的部门ID?

2 个答案:

答案 0 :(得分:1)

ViewModel表示它是DTO,数据传输对象,它不是数据库实体,而是具有您要定位的特定视图所需属性的类。
在大多数情况下,每个视图应该有一个ViewModel,并且不应该在多个视图中重用相同的模型。

因此,当您需要Name时,使用DepartmentName创建StudentXModel,当需要Id时,使用DepartmentId创建StudentYModel。

See more here

答案 1 :(得分:1)

使用ViewModel

public class StudentViewModel
{
    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; }

    public int DepartmentID { get; set; }
    public String DepartmentName { get; set; }
}

在控制器中填写此ViewModel,而不是返回Student返回StudentViewModel。请务必填写逻辑中的DepartmentName属性(此时,您应该可以使用DepartmentName 获取DepartmentID值。