Asp.net MVC视图具有多对多关系 [英] Asp.net mvc views for many to many relationship

查看:117
本文介绍了Asp.net MVC视图具有多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚安,我创建了几个模型,这些模型实现了多对多关系;现在,我在正确格式化/设计创建和编辑"视图时遇到问题. 这是我的模特:

Good day guys, I created few models that implement a many to many relationship; now I'm having problems correctly formatting/designing the 'Create and Edit' views. Here are my models:

学生模型

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

一个学生可以有多个监护人,一个监护人可以有多个学生.如何格式化创建"视图以输入这些相关对象?

A student can have multiple guardians and a guardian multiple students. How do I format the 'Create' view to enter these related objects?

推荐答案

您可以如下所示设计UI.

You can design UI as shown below.

注意::监护人"是一个下拉菜单,可以通过多选选择多个监护人.您必须为此使用多选下拉菜单.

Note : Here Guardians is a drop down where can be selected more than one Guardian by multiselecting.You have to use multiselecting dropdown for that.

您可以在此处了解更多信息:

You can read more about this here : Many to Many Relationship : a step by step View Model approach

这篇关于Asp.net MVC视图具有多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆