【发布时间】:2016-11-17 19:24:43
【问题描述】:
大家好,我创建了几个实现多对多关系的模型;现在我在正确格式化/设计“创建和编辑”视图时遇到问题。 这是我的模型:
学生模型
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; }
}
}
一个学生可以有多个监护人,一个监护人可以有多个学生。如何格式化“创建”视图以输入这些相关对象?
【问题讨论】:
-
如果我得到了你想要的,我想你应该有 2 个视图。其中之一是从学生的角度来看,用户将选择与他关联的监护人。第二个,以类似的方式,允许用户选择他想与特定监护人关联的学生。
-
@Luty 这正是我想做的,但不知道如何实现它。
-
您的型号名称错误。可能是拼写错误。
-
@Sampath 问题不在于我的模型名称。我面临的问题是创建一个监护人可以选择多个人并且一个人也可以这样做的视图。到目前为止,我发现唯一有用的链接是:asp.net/mvc/overview/getting-started/…,但这里实现的逻辑很难保留。我真的需要你们帮忙。
标签: c# asp.net asp.net-mvc entity-framework