C# 客户端验证实体框架mvc5

C# 客户端验证实体框架mvc5,c#,asp.net-mvc,entity-framework,validation,C#,Asp.net Mvc,Entity Framework,Validation,我有一个模型: public int EventID { get; set; } [Display(Name = "Name")] [Required(ErrorMessage = "You have to enter the name of the event")] public string Name { get; set; } [Display(Name = "Date")] [Required(ErrorMessage = "You h

我有一个模型:

    public int EventID { get; set; }

    [Display(Name = "Name")]
    [Required(ErrorMessage = "You have to enter the name of the event")]
    public string Name { get; set; }

    [Display(Name = "Date")]
    [Required(ErrorMessage = "You have to enter date of the event")]
    [DataType(DataType.DateTime,ErrorMessage="You have to enter valid date and time")]
    public DateTime Date { get; set; }

    [Display(Name = "Sport")]
    [Required(ErrorMessage = "You have to choose the sport")]
    public int SportID { get; set; }

    [Display(Name = "League /Competition")]
    public string SportText { get; set; }
我有表格来添加事件。 当我将日期和名称保留为空时,我会收到两个字段的错误消息(在视图中),但如果我输入日期并将名称保留为空,则不会显示错误消息,而是引发异常:

一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性


我知道我可以插入try-catch语句,但我不明白为什么客户端验证对“Name”属性不起作用。问题是,您的文本框正在传递一个空字符串值,使其通过所需的验证。

我认为这不是问题所在。我试着增加最小长度,但不起作用。当我调试它时,它的值是null而不是“”。如果我将两个文本框都留空,则两个属性(日期和名称)上都会显示错误;