2

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:

Student Model

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

Guardian Model

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 Model

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?

Sampath
  • 63,341
  • 64
  • 307
  • 441
Nathan Siafa
  • 731
  • 4
  • 19
  • 39
  • If I got what you want, I guess you should have 2 views. One of them from the perspective of the student where the user will choose the guardians to associate with him. And a second one where, in an analogous way, will allow the user to select the students he wants to associate with a specific guardian. – Luty Nov 17 '16 at 19:56
  • @Luty that is exactly what I want to do but don't know how to implement it. – Nathan Siafa Nov 17 '16 at 22:06
  • your model names are wrong.may be a typo. – Sampath Nov 19 '16 at 21:30
  • @Sampath the problem is not with my models names. The problem I faced is in creating a view in which a guardian can select multiple person and a person can do likewise. By far the only link that I found to be a little helpful is this: https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application but the logic implemented here is very difficult to withhold. I really need you guys help in doing this please. – Nathan Siafa Nov 19 '16 at 22:03

1 Answers1

0

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.

enter image description here

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

Sampath
  • 63,341
  • 64
  • 307
  • 441