2009-11-22 12 views
5

क्या कोई पैसा या मुद्रा स्वरूपित मान पार्स करने के लिए UpdateModel या TryUpdateModel प्राप्त करने का कोई तरीका है जैसे $ 1,200.00 दशमलव में भाग के बिना दशमलव में?एक मुद्रा स्वरूपित मूल्य के साथ TryUpdateModel?

+0

क्या मैंने स्टैक स्टंप किया है? ऐसा लगता है कि यह मुश्किल नहीं होना चाहिए? –

उत्तर

3

कस्टम मॉडल बाइंडर का उपयोग करने के लिए निम्न विधि का उपयोग कर सकते हैं।

An example of using one to parse a decimals differently

+0

जितना आसान या सुरुचिपूर्ण नहीं था उतना आसान था, लेकिन कुछ tweaking के साथ यह वास्तव में काम किया, तो धन्यवाद। मैं अपने tweaked बांधने की मशीन पोस्ट करेंगे। –

1

क्या आप इनमें से किसी भी विधि को कॉल करने से पहले मूल्य को आगे बढ़ाने में सक्षम हैं? यदि ऐसा है, तो आप

var provider = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone(); 
    provider.CurrencySymbol = "$"; 
    var x = decimal.Parse(
     "$1,200", 
     NumberStyles.AllowCurrencySymbol | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, 
     provider); 
+0

यह मुझे लगता है कि एक एचटीएमएल सहायक – griegs

+0

अच्छी तरह से इसे सामान्य रूप से पार्सिंग करना कोई समस्या नहीं है, लेकिन मेरे पास कई "धन" फ़ील्ड हैं और यदि संभव हो तो मैं TryUpdateModel के चारों ओर पार्सिंग के अपने नियंत्रकों का जंक नहीं करना चाहूंगा । –

+0

@ कैडमियम एक कस्टम मॉडल बाइंडर का उपयोग करें, मेरे उत्तर में लिंक देखें। – eglasius

2

उत्तर फ्रेडी रियोस को सम्मानित किया गया के बाद से अपने लिंक मुझे आधार यह करने के लिए के साथ प्रदान की है, लेकिन कोड ऊपर फिक्सिंग कुछ की जरूरत:

// http://www.crydust.be/blog/2009/07/30/custom-model-binder-to-avoid-decimal-separator-problems/ 
public class MoneyParsableModelBinder : DefaultModelBinder 
{ 

    public override object BindModel(ControllerContext controllerContext, 
     ModelBindingContext bindingContext) 
    { 

     object result = null; 
     // Added support for decimals and nullable types - c. 
     if (
      bindingContext.ModelType == typeof(double) 
      || bindingContext.ModelType == typeof(decimal) 
      || bindingContext.ModelType == typeof(double?) 
      || bindingContext.ModelType == typeof(decimal?) 
      ) 
     { 

      string modelName = bindingContext.ModelName; 
      string attemptedValue = bindingContext.ValueProvider[modelName].AttemptedValue; 

      // Depending on cultureinfo the NumberDecimalSeparator can be "," or "." 
      // Both "." and "," should be accepted, but aren't. 
      string wantedSeperator = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator; 
      string alternateSeperator = (wantedSeperator == "," ? "." : ","); 

      if (attemptedValue.IndexOf(wantedSeperator) == -1 
       && attemptedValue.IndexOf(alternateSeperator) != -1) 
      { 
       attemptedValue = attemptedValue.Replace(alternateSeperator, wantedSeperator); 
      } 

      // If SetModelValue is not called it may result in a null-ref exception if the model is resused - c. 
      bindingContext.ModelState.SetModelValue(modelName, bindingContext.ValueProvider[modelName]); 

      try 
      { 
       // Added support for decimals and nullable types - c. 
       if (bindingContext.ModelType == typeof(double) || bindingContext.ModelType == typeof(double?)) 
       { 
        result = double.Parse(attemptedValue, NumberStyles.Any); 
       } 
       else 
       { 
        result = decimal.Parse(attemptedValue, NumberStyles.Any); 
       } 
      } 
      catch (FormatException e) 
      { 
       bindingContext.ModelState.AddModelError(modelName, e); 
      } 
     } 
     else 
     { 
      result = base.BindModel(controllerContext, bindingContext); 
     } 

     return result; 
    } 
} 

यह बहुत नहीं है, लेकिन यह काम करता है।

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