ASP.NET MVC 5外键可空无法正常工作

时间:2017-06-06 18:51:50

标签: asp.net entity-framework asp.net-mvc-5

在下面的代码中,您会看到“产品”和“供应商”。在产品“SupplierID”中不是必需的。但是,如果我启动我的应用程序并且我想添加新产品,我无法将其设置为null。 我已经尝试将模型更改为“public int?SupplierID {get; set;}”。这不起作用:(在数据库中,SupplierID可以为空。请帮助。

 public class Product
 {
    public int ProductID { get; set; }

    [Required(ErrorMessage = "Artikelnummer is verplicht")]
    [StringLength(255, MinimumLength = 1)]
    [Display(Name = "Artikel nummer")]
    public string ArticleNr { get; set; }

    [Required(ErrorMessage = "Naam is verplicht")]
    [StringLength(255, MinimumLength = 1)]
    [Display(Name = "Productnaam")]
    public string Name { get; set; }

    [StringLength(255, MinimumLength = 1)]
    [Display(Name = "Type")]
    public string Type { get; set; }

    [StringLength(255, MinimumLength = 1)]
    [Display(Name = "Kleur")]
    public string Color { get; set; }

    [StringLength(255, MinimumLength = 1)]
    [Display(Name = "Lengte")]
    public string Length { get; set; }

    [Required(ErrorMessage = "Beschrijving is verplicht")]
    [StringLength(255, MinimumLength = 1)]
    [Display(Name = "Beschrijving")]
    public string Description { get; set; }

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

    public int BrandID { get; set; }
    public int CategoryID { get; set; }
    public int SupplierID { get; set; }
    public int ProjectID { get; set; }

    public Brand Brand { get; set; }
    public Category Category { get; set; }
    public Supplier Supplier { get; set; }
    public Project Project { get; set; }

    public ICollection<Inventory> Inventorys { get; set; }
}

public class Project
{
    public int ProjectID { get; set; }

    [Required(ErrorMessage = "Projectnaam is verplicht")]
    [StringLength(255, MinimumLength = 1)]
    [Display(Name = "Projectnaam")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Projectleider is verplicht")]
    [StringLength(255, MinimumLength = 1)]
    [Display(Name = "Projectleider")]
    public string ProjectLeader { get; set; }

    [Required(ErrorMessage = "Telefoonnummer is verplicht")]
    [Phone(ErrorMessage = "Voer een geldig telefoonnummer in")]
    [StringLength(255, MinimumLength = 1)]
    [Display(Name = "Telefoonnummer")]
    public string PhoneNr { get; set; }

    [Required(ErrorMessage = "Email is verplicht")]
    [EmailAddress(ErrorMessage = "Voer een geldig e-mail in")]
    [StringLength(255, MinimumLength = 1)]
    [Display(Name = "E-mail")]
    public string Email { get; set; }

    public ICollection<Product> Products { get; set; }
}

0 个答案:

没有答案