真布尔值所需的属性MVC的.NET属性 [英] Boolean value of True for required attribute on MVC .net property
问题描述
我如何需要的布尔
属性MVC 3与.NET中的真
的价值?
这是我在哪里,我需要值为真
othewise这不是有效的
<必需的()> _
<显示名称(协议接受)> _
公共属性AcceptAgreement由于布尔
下面是情况下修复链接模具哪天
添加该类
公共类BooleanMustBeTrueAttribute继承ValidationAttribute 公共覆盖功能的IsValid(BYVAL为PropertyValue作为对象)为布尔
返回为PropertyValue IsNot运算没有AndAlso运算TypeOf运算为PropertyValue是布尔AndAlso运算CBool将(为PropertyValue)
结束功能末级
添加属性
<必需的()> _
<显示名称(协议接受)> _
< BooleanMustBeTrue(的ErrorMessage:=您必须同意的条款和条件)> _
公共属性AcceptAgreement由于布尔
如果任何人有兴趣加入jQuery验证(这样复选框无论是在浏览器和服务器进行验证),你应该修改BooleanMustBeTrueAttribute类,像这样:
公共类BooleanMustBeTrueAttribute:ValidationAttribute,IClientValidatable
{
公众覆盖BOOL的IsValid(对象为PropertyValue)
{
返回为PropertyValue!= NULL
&功放;&安培;为PropertyValue是布尔
&功放;&安培; (布尔)为PropertyValue;
} 公共IEnumerable的< ModelClientValidationRule> GetClientValidationRules(ModelMetadata元,ControllerContext上下文)
{
收益回报新ModelClientValidationRule
{
的ErrorMessage = this.ErrorMessage,
ValidationType =mustbetrue
};
}
}
基本上,类现在还实现IClientValidatable,并返回相应的js错误信息,将被添加到HTML域(mustbetrue)。
的jQuery验证属性现在,为了让jQuery验证工作,添加以下的js到页面:
jQuery.validator.addMethod('mustBeTrue',功能(价值){
返回值; //我们不需要别的任何检查,因为我们想要的值是真实的。
},'');//和一个不显眼的适配器
jQuery.validator.unobtrusive.adapters.add('mustbetrue',{},功能(选件){
options.rules ['mustBeTrue'] =真;
options.messages ['mustBeTrue'] = options.message;
});
注:我根据这个答案中使用的一个previous code - > <一个href=\"http://stackoverflow.com/questions/4747184/perform-client-side-validation-for-custom-attribute\">Perform客户端验证为自定义属性
而这基本上它:)
记住,对于previous JS工作,您必须包含在页面下面的js文件:
&LT;脚本的src =@ Url.Content(〜/脚本/ jquery.validate.js)TYPE =文/ JavaScript的&GT;&LT; / SCRIPT&GT;
&LT;脚本的src =@ Url.Content(〜/脚本/ jquery.validate.unobtrusive.js)TYPE =文/ JavaScript的&GT;&LT; / SCRIPT&GT;
P.S。当你有工作,我真的建议加入code在脚本文件夹中的js文件,并创建一个捆绑的所有js文件。
How do I require a value of True
for a boolean
property in MVC 3 with .NET?
This is where I am, I need the value to be True
othewise it's not valid
<Required()> _
<DisplayName("Agreement Accepted")> _
Public Property AcceptAgreement As Boolean
Here is the fix in case the link dies someday
Add this class
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
Add attribute
<Required()> _
<DisplayName("Agreement Accepted")> _
<BooleanMustBeTrue(ErrorMessage:="You must agree to the terms and conditions")> _
Public Property AcceptAgreement As Boolean
If anyone is interested in adding jquery validation (so that the checkbox is validated both in the browser and the server) you should modify the BooleanMustBeTrueAttribute class like so:
public class BooleanMustBeTrueAttribute : ValidationAttribute, IClientValidatable
{
public override bool IsValid(object propertyValue)
{
return propertyValue != null
&& propertyValue is bool
&& (bool)propertyValue;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRule
{
ErrorMessage = this.ErrorMessage,
ValidationType = "mustbetrue"
};
}
}
Basically, the class now also implements IClientValidatable, and returns the corresponding js error message and the jquery validation attribute that will be added to HTML field ("mustbetrue").
Now, in order for the jquery validation to work, add the following js to the page:
jQuery.validator.addMethod('mustBeTrue', function (value) {
return value; // We don't need to check anything else, as we want the value to be true.
}, '');
// and an unobtrusive adapter
jQuery.validator.unobtrusive.adapters.add('mustbetrue', {}, function (options) {
options.rules['mustBeTrue'] = true;
options.messages['mustBeTrue'] = options.message;
});
Note: I based the previous code on the one used in this answer -> Perform client side validation for custom attribute
And that's basically it :)
Remember that for the previous js to work, you must have included the following js files in the page:
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
P.S. When you have it working, I would actually recommend adding the code to a js file in the Scripts folder, and create a bundle with all the js files.
这篇关于真布尔值所需的属性MVC的.NET属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!