簡體   English   中英

MVC5剃須刀中復選框的自定義驗證

[英]Custom Validation for Checkbox in MVC5 razor

我試圖強制查看所需的條款和條件復選框。 我遵循了該論壇上概述的代碼示例: http : //www.dotnet-tricks.com/Tutorial/mvc/8UFa191212-Custom-Validation-for-Checkbox-in-MVC-Razor.html,但是如果我在其中設置了斷點IsValid方法,是否從未實現過。 同樣,當ModelState.IsValid為false時,它返回true; 如果從未選中該復選框。

這是ViewModel的代碼:

public class TermsConditionViewModel
{
    [MustBeTrue(ErrorMessage = "The terms and conditions must be read and agreed to complete the registration process.")]
    [Display(Name = "")]
    public bool TermsConditionsCompleted { get; set; }
} 

這是自定義數據注釋的代碼:

public class MustBeTrueAttribute : ValidationAttribute, IClientValidatable // IClientValidatable for client side Validation
{
    public override bool IsValid(object value)
    {
        return value is bool && (bool)value;
    }
    // Implement IClientValidatable for client side Validation
    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        return new ModelClientValidationRule[] { new ModelClientValidationRule { ValidationType = "checkbox", ErrorMessage = this.ErrorMessage } };
    }


}

public class MustBeSelected : ValidationAttribute, IClientValidatable // IClientValidatable for client side Validation
{
    public override bool IsValid(object value)
    {
        if (value == null || (int)value == 0)
            return false;
        else
            return true;
    }
    // Implement IClientValidatable for client side Validation
    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        return new ModelClientValidationRule[] { new ModelClientValidationRule { ValidationType = "dropdown", ErrorMessage = this.ErrorMessage } };
    }
}

這是客戶端驗證的JavaScript代碼

$.validator.unobtrusive.adapters.add("dropdown", function (options) {
//  debugger;
if (options.element.tagName.toUpperCase() == "SELECT" && options.element.type.toUpperCase() == "SELECT-ONE") {
    options.rules["required"] = true;
    if (options.message) {
        options.messages["required"] = options.message;
    }
}
});

$.validator.unobtrusive.adapters.add("checkbox", function (options) {
if (options.element.tagName.toUpperCase() == "INPUT" && options.element.type.toUpperCase() == "CHECKBOX") {
    options.rules["required"] = true;
    if (options.message) {
        options.messages["required"] = options.message;
    }
}
});

這是生成的標記

<input data-val="true" data-val-checkbox="The terms and conditions must be read and agreed to complete the registration process." data-val-required="The  field is required." id="TermsConditionsCompleted" name="TermsConditionsCompleted" type="checkbox" value="true" /><input name="TermsConditionsCompleted" type="hidden" value="false" />&nbsp;I have read and agree with the terms and conditions described above

要進行此工作,是否需要進行任何其他配置? 到目前為止,我發現的所有示例似乎都是以相同的方式設置的,Web.Config,Global.asax等中沒有配置。

您缺少一些jQuery驗證步驟。 我已經按照此鏈接上的路徑解決了相同的問題並完成了相同的任務: http : //www.yogihosting.com/client-side-validation-asp-net-mvc/

希望對您有幫助。

亞歷山德羅

您的解決方案似乎實在是太過分了,相反,我只會在表單提交上添加一個jQuery方法,以確保選中該復選框。

更少的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM