在视图中使用许多模型类

时间:2016-05-05 11:17:46

标签: c# asp.net-mvc

我有那个模特课:

public class CV
{
 public PrivateInformation privateInformation { get; set; }
 public List<Education> education { get; set; }
 public List<WorkingExperience> workingExperience { get; set; }
}

public class PrivateInformation
{
public string Name { get; set; }
public DateTime BirthDate { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
}

public class Education
{
public string UniversityName { get; set; }
public string Faculty { get; set; }
public string Specialization { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool OnGoing { get; set; }
}

public class WorkingExperience
{
public string CompanyName { get; set; }
public string Position { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool OnGoing { get; set; }
}

还有其他课程。我想让该用户从一个页面输入完整的信息。 (大学可以超过1,因为我也有工作经验。)

但是当我在视图中将CV作为模型类时,我无法访问Education.Name

我该怎么做?或者还有其他方法吗?

2 个答案:

答案 0 :(得分:0)

这样做的常见模式是创建ViewModel类

基本上,创建一个包含视图所需的所有属性的类,然后使用它。

https://msdn.microsoft.com/en-us/library/ff798384.aspx

答案 1 :(得分:0)

如果你想使用其他类似的东西,只需创建一个整个模型的模型并初始化其他类的属性,

Public class PrivateInformationViewModel
{

ublic string UniversityName { get; set; }
public string Faculty { get; set; }
public string Specialization { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool OnGoing { get; set; }
public Education EModel {get; set;}

}

现在通过此视图模型,您还可以访问教育属性。