【发布时间】:2011-10-28 14:45:02
【问题描述】:
如何在带有 .NET 的 MVC 3 中要求 boolean 属性的值为 True?
这就是我所在的位置,我需要将值设为 True 否则它无效
<Required()> _
<DisplayName("Agreement Accepted")> _
Public Property AcceptAgreement As Boolean
这是链接在某天失效时的修复方法
添加这个类
Public Class BooleanMustBeTrueAttribute Inherits ValidationAttribute
Public Overrides Function IsValid(ByVal propertyValue As Object) As Boolean
Return propertyValue IsNot Nothing AndAlso TypeOf propertyValue Is Boolean AndAlso CBool(propertyValue)
End Function
End Class
添加属性
<Required()> _
<DisplayName("Agreement Accepted")> _
<BooleanMustBeTrue(ErrorMessage:="You must agree to the terms and conditions")> _
Public Property AcceptAgreement As Boolean
【问题讨论】:
-
我知道这是旧的,但如果你添加你的解决方案作为答案,我会赞成:)
标签: asp.net-mvc