如何使用 DataAnnotations 处理 ASP.NET MVC 2 中的布尔值/复选框?

问题描述 投票:0回答:13

我有一个像这样的视图模型:

public class SignUpViewModel
{
    [Required(ErrorMessage = "Bitte lesen und akzeptieren Sie die AGB.")]
    [DisplayName("Ich habe die AGB gelesen und akzeptiere diese.")]
    public bool AgreesWithTerms { get; set; }
}

视图标记代码:

<%= Html.CheckBoxFor(m => m.AgreesWithTerms) %>
<%= Html.LabelFor(m => m.AgreesWithTerms)%>

结果:

不执行任何验证。到目前为止还可以,因为 bool 是一个值类型并且永远不会为 null。但即使我将 AgreesWithTerms 设置为可为空,它也不会起作用,因为编译器会喊

“模板只能与字段访问、属性访问、一维数组索引或单参数自定义索引器表达式一起使用。”

那么,处理这个问题的正确方法是什么?

asp.net-mvc validation checkbox data-annotations
13个回答
99
投票

我的解决方案如下(与已经提交的答案没有太大区别,但我相信它的命名更好):

/// <summary>
/// Validation attribute that demands that a boolean value must be true.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class MustBeTrueAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        return value != null && value is bool && (bool)value;
    }
}

然后你可以在你的模型中像这样使用它:

[MustBeTrue(ErrorMessage = "You must accept the terms and conditions")]
[DisplayName("Accept terms and conditions")]
public bool AcceptsTerms { get; set; }

51
投票

我将为服务器端和客户端创建一个验证器。使用 MVC 和不显眼的表单验证,只需执行以下操作即可实现此目的:

首先,在项目中创建一个类来执行服务器端验证,如下所示:

public class EnforceTrueAttribute : ValidationAttribute, IClientValidatable
{
    public override bool IsValid(object value)
    {
        if (value == null) return false;
        if (value.GetType() != typeof(bool)) throw new InvalidOperationException("can only be used on boolean properties.");
        return (bool)value == true;
    }

    public override string FormatErrorMessage(string name)
    {
        return "The " + name + " field must be checked in order to continue.";
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        yield return new ModelClientValidationRule
        {
            ErrorMessage = String.IsNullOrEmpty(ErrorMessage) ? FormatErrorMessage(metadata.DisplayName) : ErrorMessage,
            ValidationType = "enforcetrue"
        };
    }
}

接下来,在模型中注释适当的属性:

[EnforceTrue(ErrorMessage=@"Error Message")]
public bool ThisMustBeTrue{ get; set; }

最后,通过将以下脚本添加到您的视图来启用客户端验证:

<script type="text/javascript">
    jQuery.validator.addMethod("enforcetrue", function (value, element, param) {
        return element.checked;
    });
    jQuery.validator.unobtrusive.adapters.addBool("enforcetrue");
</script>

注意:我们已经创建了一个方法

GetClientValidationRules
,它将我们的注释从模型推送到视图。


20
投票

我通过创建自定义属性得到它:

public class BooleanRequiredAttribute : RequiredAttribute 
{
    public override bool IsValid(object value)
    {
        return value != null && (bool) value;
    }
}

8
投票

这可能是一个“黑客”,但您可以使用内置的 Range 属性:

[Display(Name = "Accepted Terms Of Service")]
[Range(typeof(bool), "true", "true")]
public bool Terms { get; set; }

唯一的问题是“警告”字符串会显示“FIELDNAME 必须介于 True 和 true 之间”。


7
投票
[Compare("Remember", ErrorMessage = "You must accept the terms and conditions")]
public bool Remember { get; set; }

6
投票

我只是采用现有解决方案中最好的,并将其组合成一个单一的答案,允许服务器端和客户端验证。

应用于对属性建模以确保 bool 值必须为 true:

/// <summary>
/// Validation attribute that demands that a <see cref="bool"/> value must be true.
/// </summary>
/// <remarks>Thank you <c>http://stackoverflow.com/a/22511718</c></remarks>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class MustBeTrueAttribute : ValidationAttribute, IClientValidatable
{
    /// <summary>
    /// Initializes a new instance of the <see cref="MustBeTrueAttribute" /> class.
    /// </summary>
    public MustBeTrueAttribute()
        : base(() => "The field {0} must be checked.")
    {
    }

    /// <summary>
    /// Checks to see if the given object in <paramref name="value"/> is <c>true</c>.
    /// </summary>
    /// <param name="value">The value to check.</param>
    /// <returns><c>true</c> if the object is a <see cref="bool"/> and <c>true</c>; otherwise <c>false</c>.</returns>
    public override bool IsValid(object value)
    {
        return (value as bool?).GetValueOrDefault();
    }

    /// <summary>
    /// Returns client validation rules for <see cref="bool"/> values that must be true.
    /// </summary>
    /// <param name="metadata">The model metadata.</param>
    /// <param name="context">The controller context.</param>
    /// <returns>The client validation rules for this validator.</returns>
    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        if (metadata == null)
            throw new ArgumentNullException("metadata");
        if (context == null)
            throw new ArgumentNullException("context");

        yield return new ModelClientValidationRule
            {
                ErrorMessage = FormatErrorMessage(metadata.DisplayName),
                ValidationType = "mustbetrue",
            };
    }
}

要包含的 JavaScript 以利用不显眼的验证。

jQuery.validator.addMethod("mustbetrue", function (value, element) {
    return element.checked;
});
jQuery.validator.unobtrusive.adapters.addBool("mustbetrue");

4
投票

此处“必需”是错误的验证。您想要类似于“必须具有 true 值”的内容,这与“必需”不同。使用类似的东西怎么样:

[RegularExpression("^true")]


3
投票

我的解决方案是这个简单的布尔值自定义属性:

public class BooleanAttribute : ValidationAttribute
{
    public bool Value
    {
        get;
        set;
    }

    public override bool IsValid(object value)
    {
        return value != null && value is bool && (bool)value == Value;
    }
}

然后你可以在你的模型中像这样使用它:

[Required]
[Boolean(Value = true, ErrorMessage = "You must accept the terms and conditions")]
[DisplayName("Accept terms and conditions")]
public bool AcceptsTerms { get; set; }

3
投票

正确的方法是检查类型!

[Range(typeof(bool), "true", "true", ErrorMessage = "You must or else!")]
public bool AgreesWithTerms { get; set; }

2
投票

对于在客户端验证时遇到困难的人(以前的我):请确保您也有

  1. 包含在视图中的表单之前<% Html.EnableClientValidation(); %>
  2. 二手<%= Html.ValidationMessage or Html.ValidationMessageFor for the field
  3. 创建了一个 DataAnnotationsModelValidator,它返回具有自定义验证类型的规则
  4. 在 Global.Application_Start 中注册了从 DataAnnotationsModelValidator 派生的类

http://www.highoncoding.com/Articles/729_Creating_Custom_Client_Side_Validation_in_ASP_NET_MVC_2_0.aspx

这是一个很好的教程,但错过了第 4 步。


1
投票

在这里找到了更完整的解决方案(服务器端和客户端验证):

http://blog. Degree.no/2012/03/validation-of-required-checkbox-in-asp-net-mvc/#comments


1
投票

添加【正则表达式】即可:

[DisplayName("I accept terms and conditions")]
[RegularExpression("True", ErrorMessage = "You must accept the terms and conditions")]
public bool AgreesWithTerms { get; set; }

注意 - “True”必须以大写 T 开头


0
投票

如果您使用 s1mm0t 针对 .NET Core 6 及更高版本的答案 您还需要添加必需的标签,所以:

注册.cshtml.cs:

public class RegisterModel : PageModel{
...
 /// <summary>
        /// Validation attribute that demands that a boolean value must be true.
        /// </summary>
        [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
        public class MustBeTrueAttribute : ValidationAttribute
        {
            public override bool IsValid(object value)
            {
                return value != null && value is bool && (bool)value;
            }
        }
    ...

}

public class InputModel{
...
[Required]
            [MustBeTrue(ErrorMessage = "You must accept the terms and conditions\n")]
            [DisplayName("Accept terms and conditions")]
            public bool AcceptsTerms { get; set; }
}

注册.cshtml:

<p>
                    <input id="AcceptTermsCheckbox" asp-for="Input.AcceptsTerms" />
                    <label asp-for="Input.AcceptsTerms"></label>
                    <span asp-validation-for="Input.AcceptsTerms" class="text-danger"></span>
                    By creating an account you confirm that you have read and agree with our Terms & Conditions and Privacy Policy
                </p>
© www.soinside.com 2019 - 2024. All rights reserved.