Asp.net mvc对很多很多关系的看法

时间:2016-11-17 19:24:43

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

美好的一天,我创造了几个实现多对多关系的模型;现在我在格式化/设计'创建和编辑'时遇到了问题。观点。 这是我的模特:

学生模特

namespace HMS.Models
{
    [Table("Students", Schema ="Admission")]
    public class Students : Person
    {
        [Key]
        public int StudentId { get; set; }

        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Display(Name = "Last Name")]
        public string LastName { get; set; }               

        // this associate a student with a list of guardian  
        public virtual ICollection<Guardian> Guardians { get; set; }
    }
}

守护者模型

namespace HMS.Models
{
    [Table("Guardians", Schema ="Admission")]
    public class Students : Person
    {
        [Key]
        public int GuardianId { get; set; }

        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Display(Name = "Last Name")]
        public string LastName { get; set; }               

        // this associate a student with a list of guardian  
        public virtual ICollection<Student> Students { get; set; }
    }
}

StudentGuardian模型

namespace HMS.Models
{
    [Table("StudentGuardian", Schema ="Admission")]
    public class Students : Person
    {
        [Key]
        public int Id { get; set; }

        [Display(Name = "Guardian Id")]
        [ForeignKey("GuardianId")]
        public int GuardianId { get; set; }

        [Display(Name = "Student Id")]
        [ForeignKey("StudentId")]
        public string StudentId { get; set; }               
    }
}

学生可以有多名监护人和一名监护人多名学生。如何格式化&#39;创建&#39;查看输入这些相关对象?

1 个答案:

答案 0 :(得分:0)

您可以设计UI,如下所示。

注意:这里的Guardians是一个下拉列表,可以通过多选来选择多个Guardian。你必须使用多选下拉列表。

enter image description here

您可以在此处详细了解: Many to Many Relationship : a step by step View Model approach