Asp.net-Mvc

如何在 mvc 中添加布爾必需屬性?

  • September 30, 2014

我有一個模型類,例如:

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; }

引用自:https://stackoverflow.com/questions/26122171