2012-09-22 21 views
21

एक PropertyValueRequired कुंजी के साथ App_GlobalResources करने के लिए एक संसाधन फ़ाइल जोड़ना और फ़ाइल नाम के DefaultModelBinder.ResourceClassKey बदलते MVC 4. स्ट्रिंग The {0} field is required कभी नहीं बदल गया है पर कोई प्रभाव नहीं है। मैं प्रत्येक आवश्यक फ़ील्ड पर संसाधन वर्ग प्रकार और कुंजी सेट नहीं करना चाहता हूं। क्या मुझे कुछ याद आ रही है?MVC 4 पर ध्यान नहीं देता DefaultModelBinder.ResourceClassKey

संपादित करें:

मैं डैरिन दिमित्रोव के कोड पर एक छोटा सा संशोधन कर दिया है आवश्यक अनुकूलन काम कर रखने के लिए:

public class MyRequiredAttributeAdapter : RequiredAttributeAdapter 
{ 
    public MyRequiredAttributeAdapter(
     ModelMetadata metadata, 
     ControllerContext context, 
     RequiredAttribute attribute 
    ) 
     : base(metadata, context, attribute) 
    { 
     if (attribute.ErrorMessageResourceType == null) 
     { 
      attribute.ErrorMessageResourceType = typeof(Messages); 
     } 
     if (attribute.ErrorMessageResourceName == null) 
     { 
      attribute.ErrorMessageResourceName = "PropertyValueRequired"; 
     } 
    } 
} 

उत्तर

39

यह ASP.NET MVC 4. के लिए विशिष्ट यह था नहीं है एएसपी.नेट एमवीसी 3 में वही है। आप DefaultModelBinder.ResourceClassKey का उपयोग करके आवश्यक संदेश सेट नहीं कर सकते हैं, केवल PropertyValueInvalid। जब गैर-व्यर्थ क्षेत्र नहीं है अब

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(RequiredAttribute), 
    typeof(MyRequiredAttributeAdapter) 
); 

:

public class MyRequiredAttributeAdapter : RequiredAttributeAdapter 
{ 
    public MyRequiredAttributeAdapter(
     ModelMetadata metadata, 
     ControllerContext context, 
     RequiredAttribute attribute 
    ) : base(metadata, context, attribute) 
    { 
     attribute.ErrorMessageResourceType = typeof(Messages); 
     attribute.ErrorMessageResourceName = "PropertyValueRequired"; 
    } 
} 

कि आप Application_Start में दर्ज किए जाएंगे:

एक तरह से प्राप्त करने के लिए आप के लिए क्या देख रहे एक कस्टम RequiredAttributeAdapter परिभाषित करने के लिए है एक मान असाइन किया गया, त्रुटि संदेश Messages.PropertyValueRequired से आएगा जहां Messages.resxApp_GlobalResources के अंदर परिभाषित किया जाना चाहिए।

+0

बिल्कुल सही! मैंने बहुत सारे शोध किए और कुछ भी समान नहीं पाया। आपका बहुत बहुत धन्यवाद। – Eduardo

+0

@DarinDimitrov, आपका उत्तर बहुत अच्छा है, धन्यवाद .... – RAM

+0

क्या टाइप प्रमाणीकरण के लिए कुछ ऐसा करना संभव है, उदा। तिथियों के लिए? – Rowan

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

  • कोई संबंधित समस्या नहीं^_^