在 ASP .Net MVC 中查看模型数据
本文关键字:模型 数据 ASP Net MVC | 更新日期: 2023-09-27 18:33:51
在我的项目中有两个领域模型类:学生.cs和部门.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; }
}
现在,为了向最终用户显示全名而不是视图中的名称组件,我想出了一个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?
ViewModel 意味着它是一个 DTO、数据传输对象,它不是数据库实体,而是一个类,具有您定位的特定视图所需的属性。
在大多数情况下,每个视图应有 1 个 ViewModel,并且不应在多个视图中重复使用同一模型。
因此,当您需要名称时,请使用部门名称创建StudentXModel,在需要ID时创建带有部门ID的学生YModel。
在此处查看更多内容
使用 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; }
}
在控制器中填充此视图模型,而不是返回一个Student
返回一个StudentViewModel
。请记住在逻辑中填充 DepartmentName
属性(此时您应该能够使用 DepartmentID
获取DepartmentName
值)。