Asp.net-Mvc
如何在 mvc 中添加布爾必需屬性?
我有一個模型類,例如:
public class Student { [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] [Display(Name = "Enrollment Date")] public DateTime EnrollmentDate { get; set; } [Required] [Display(Name = "Is Active")] public bool IsActive { get; set; } public virtual ICollection<Enrollment> Enrollments { get; set; } }在這裡,我創建了一個帶有屬性的
Boolean屬性,但問題是我的視圖沒有對該屬性執行所需的驗證?我想將此屬性與 a 綁定並檢查是否已檢查,如果未檢查則執行驗證。IsActive``Required``CheckBox``CheckBox有什麼解決辦法嗎?
[Display(Name = "Is Active")] [Range(typeof(bool), "true", "true", ErrorMessage="The field Is Active must be checked.")] public bool IsActive { get; set; }