【问题标题】:use two models in one view在一个视图中使用两个模型
【发布时间】:2014-10-15 11:06:45
【问题描述】:

net MVC 5 我有一个需要来自两个模型的信息的视图。在一个模型视图中,我有学生标记,在另一个模型视图中,我有学生详细信息如何将两个模型都调用到视图中。

公开课学生 { [钥匙] 公共 int StudentNum { 得到;放; }

    public string StudentName { get; set; }

    public string StudentSurname { get; set; }

    public string Email { get; set; }
}

public class Marks
{
    [Key]
    public int Mark1 { get; set; }

    public int Mark2 { get; set; }

    public int Dp { get; set; }

    public int StudentNum { get; set; }

    public virtual Student Student { get; set; }
}

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-5


    【解决方案1】:

    创建一个新的视图模型,将其他视图模型封装为,

    public class StudentViewModel
    {
        public Details Details { get; set; }
    
        public Marks Marks { get; set; }
    }
    

    并将其传递给您使用的视图。并通过 Model.StudentViewModel.Details 访问以获取学生详细信息和标记 Model.StudentViewModel.Marks。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-24
      • 1970-01-01
      相关资源
      最近更新 更多