2011-08-08 12 views
19

का उपयोग करके मॉडल के बूलियन मान को सही होने के लिए लागू करना सरल समस्या (मुझे लगता है)।डेटा एनोटेशन

मेरे पास नीचे एक चेकबॉक्स वाला एक फॉर्म है जहां उपयोगकर्ता को नियम और शर्तों से सहमत होना चाहिए। यदि उपयोगकर्ता बॉक्स को चेक नहीं करता है, तो मैं अन्य सत्यापन त्रुटियों के साथ अपने सत्यापन सारांश में एक त्रुटि संदेश प्रदर्शित करना चाहता हूं।

मैं मेरे विचार मॉडल को यह कहा:

[Required] 
[Range(1, 1, ErrorMessage = "You must agree to the Terms and Conditions")] 
public bool AgreeTerms { get; set; } 

लेकिन वह काम नहीं किया।

क्या डेटा एनोटेशन के साथ एक मूल्य को सही करने के लिए मजबूर करने का कोई आसान तरीका है?

+0

कस्टम एनोटेशन लिखना बहुत आसान है, क्या आपने वह विकल्प माना है? – asawyer

+0

एक बूल के लिए नहीं, लेकिन बहुत समान (और कस्टम त्रुटि संदेशों के लिए अनुमति देता है): http://rical.blogspot.com/2012/03/server-side-custom-annotation.html – pkr298

उत्तर

24
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Threading.Tasks; 
using System.Web.Mvc; 

namespace Checked.Entitites 
{ 
    public class BooleanRequiredAttribute : ValidationAttribute, IClientValidatable 
    { 
     public override bool IsValid(object value) 
     { 
      return value != null && (bool)value == true; 
     } 

     public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) 
     { 
      //return new ModelClientValidationRule[] { new ModelClientValidationRule() { ValidationType = "booleanrequired", ErrorMessage = this.ErrorMessage } }; 
      yield return new ModelClientValidationRule() 
      { 
       ValidationType = "booleanrequired", 
       ErrorMessage = this.ErrorMessageString 
      }; 
     } 
    } 
} 
6

आप एक कस्टम सत्यापन विशेषता लिख ​​सकते हैं जिसका पहले ही उल्लेख किया जा चुका है। यदि आप क्लाइंट साइड सत्यापन कर रहे हैं तो आपको इसे चुनने के लिए अविभाज्य सत्यापन कार्यक्षमता को सक्षम करने के लिए कस्टम जावास्क्रिप्ट लिखना होगा। जैसे अगर आप jQuery का उपयोग कर रहे हैं:

// extend jquery unobtrusive validation 
(function ($) { 

    // add the validator for the boolean attribute 
    $.validator.addMethod(
    "booleanrequired", 
    function (value, element, params) { 

     // value: the value entered into the input 
     // element: the element being validated 
     // params: the parameters specified in the unobtrusive adapter 

     // do your validation here an return true or false 

    }); 

    // you then need to hook the custom validation attribute into the MS unobtrusive validators 
    $.validator.unobtrusive.adapters.add(
    "booleanrequired", // adapter name 
    ["booleanrequired"], // the names for the properties on the object that will be passed to the validator method 
    function(options) { 

     // set the properties for the validator method 
     options.rules["booleanRequired"] = options.params; 

     // set the message to output if validation fails 
     options.messages["booleanRequired] = options.message; 

    }); 

} (jQuery)); 

एक और तरीका है (जो एक हैक का एक सा है और मैं इसे पसंद नहीं है) है अपने मॉडल पर एक संपत्ति है कि हमेशा सच करने के लिए सेट कर दिया जाता है, तो का उपयोग अपने * AgreeTerms * विशेषता के मान की तुलना करने के लिए तुलना करें। सरल हाँ, लेकिन मुझे यह पसंद नहीं है :)

+0

कुंजी केस संवेदनशील हैं: बूलियनरेक्टेड बूलियन आवश्यक –

संबंधित मुद्दे