asp.net mvc 2客户端验证缺少自定义属性上的ValidationRules

asp.net mvc 2客户端验证缺少自定义属性上的ValidationRules,validation,asp.net-mvc-2,checkbox,client-side,Validation,Asp.net Mvc 2,Checkbox,Client Side,似乎无法使用asp.net mvc 2在客户端验证复选框。这是我的密码 模型 自定义属性 public class RequiredToBeTrueAttribute : RequiredAttribute { public override bool IsValid(object value) { return (value != null) && (value is bool) ? (bool)value : false; } } 看

似乎无法使用asp.net mvc 2在客户端验证复选框。这是我的密码

模型

自定义属性

public class RequiredToBeTrueAttribute : RequiredAttribute
{
    public override bool IsValid(object value)
    {
        return (value != null) && (value is bool) ? (bool)value : false;
    }
}
看法

您可以看到“ValidationRules”是空的,这意味着它正在尝试验证它,但由于某种原因,错误消息没有发送到客户端


有什么想法吗?非常感谢您的帮助。

看来我需要先做更多的挖掘工作。希望新属性能神奇地出现在客户端。相反,必须编写一些客户javascript来连接它。有关详细信息,请参阅菲尔·哈克的文章。

菲尔·哈克的这篇文章将帮助您找到正确的方向

基本上,您需要创建自己的
DataAnnotationsModelValidator
,然后编写一些客户端脚本来完成

HTHs,
查尔斯

public class RequiredToBeTrueAttribute : RequiredAttribute
{
    public override bool IsValid(object value)
    {
        return (value != null) && (value is bool) ? (bool)value : false;
    }
}
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<RegistrationModel>" %>

 <% Html.EnableClientValidation(); %>
 <% using (Html.BeginForm("Register", "Registration", new { area="Account", id = "openid_form", inRegistration = true }))
 <%=Html.ValidationSummary(false)  %>
 blah blah blah
 <div class="checkbox"><label><%= Html.CheckBoxFor(model => model.TermsAndConditions) %>I agree to the <a href="content/terms-conditions.html" id="terms-contents">terms and conditions</a> of use.</label></div>
 <input type="submit" id="submit" name="submit" value="Join Now" />
 <%
    Html.ValidateFor(m => m.TermsAndConditions);            
 %>
 <% } %>      
{"FieldName":"TermsAndConditions","ReplaceValidationMessageContents":false,"ValidationMessageId":null,"ValidationRules":[]}