0

我创建了一个名为 tbGeneralOptions 的表,其中包含对我的应用程序中问题的一些默认答案。选项为 Yes、No、N/A,其值分别为 1、2、3。

public class tbGeneralOption
{
    public int tbGeneralOptionID { get; set; }

    [Required(ErrorMessage = "Please enter an Option")]
    [Display(Name = "Option")]
    public string OptionName { get; set; }

    public virtual ICollection<tbFamilyMember> tbFamilyMember { get; set; }
    public virtual ICollection<tbFamily> tbFamily { get; set; }

}

在我的 tbFamilyMember 表中,我将上述选项中的值存储在 Disability 和 PhotoPermission 字段中。

public class tbFamilyMember
{
    [Display(Name = "Family Member ID")]
    public int tbFamilyMemberID { get; set; }

    [Display(Name = "Family ID")]
    public int tbFamilyID { get; set; }

    [Required(ErrorMessage = "Please select a Role")]
    [Display(Name = "Role")]
    public int tbFamilyRoleID { get; set; }

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

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

    [Required(ErrorMessage = "Please select if the Family Member has a disability")]
    [Display(Name = "Disability")]
    public int Disability { get; set; }

    [Required(ErrorMessage = "Please select if the Family Member has given permission for photographs")]
    [Display(Name = "Photo Permission")]
    public int PhotoPermission { get; set; }

    public virtual tbFamily tbFamily { get; set; }
    public virtual tbFamilyRole tbFamilyRole { get; set; }
    public virtual tbGeneralOption tbGeneralOption { get; set; }
}

我现在的问题是如何在我的视图中显示存储值的 OptionName。

我假设我可以通过外键建立链接,但如果字段名称不匹配,可以这样做吗?

4

1 回答 1

0

根据您在下面的评论,您根本不需要tbGeneralOption,因为您的每种类型的选项是固定的和静态的 - 选项不是动态添加的。因此,您应该在具有选项的类型上创建选项作为布尔属性。此外,您可以删除这些选项的错误消息,因为根据定义,选项不是必需的。

我删除了属性tbFamilyIDtbFamilyRoleID因为您已经在底部引用了 family 和 familyRole 并且可以在此处访问 ID,以防您需要显示它们,而您可能不应该这样做。这是家庭成员的更新模型:

public class tbFamilyMember
{
    public int tbFamilyMemberID { get; set; }

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

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

    [Display(Name = "Disability")]
    public bool Disability { get; set; }

    [Display(Name = "Photo Permission")]
    public bool PhotoPermission { get; set; }

    public virtual tbFamily tbFamily { get; set; }
    public virtual tbFamilyRole tbFamilyRole { get; set; }
}
于 2013-04-18T14:59:05.627 回答