似乎無法使用asp.net mvc 2在客戶端驗證複選框。以下是我的代碼。asp.net mvc 2客戶端驗證缺少自定義屬性上的驗證規則
型號
[Serializable]
public class RegistrationModel
{
bool termsAndCondition = false;
[RequiredToBeTrue(ErrorMessage = "Need terms and service")]
public bool TermsAndConditions
{
get
{
return termsAndCondition;
}
set
{
termsAndCondition = value;
}
}
}
自定義屬性
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);
%>
<% } %>
我想打電話Html.ValidateFor末在頂部推了所有的錯誤信息頁。但是,「TermsAndConditions」屬性在客戶端沒有得到驗證(在服務器端很有效)。這使我看的window.mvcClientValidationMetData方法在那個MVC推了,我看到了以下內容:
{"FieldName":"TermsAndConditions","ReplaceValidationMessageContents":false,"ValidationMessageId":null,"ValidationRules":[]}
,你可以看到「ValidationRules」是,它是試圖驗證它毫無意義,但出於某種原因,錯誤消息沒有推送給客戶端。
任何想法?任何幫助表示讚賞。