2009-11-22 54 views
5

Có cách nào để tải UpdateModel hoặc TryUpdateModel để phân tích một giá trị tiền hoặc tiền tệ được định dạng, chẳng hạn như $ 1,200.00 thành số thập phân mà không cần thổi khối không?TryUpdateModel với giá trị được định dạng tiền tệ?

+0

Tôi có xếp chồng ngăn xếp không? Nó không có vẻ như nó phải là khó khăn? –

Trả lời

3

Sử dụng chất kết dính mô hình tùy chỉnh.

An example of using one to parse a decimals differently

+0

Không đơn giản hay thanh lịch như tôi đã hy vọng, nhưng với một số tinh chỉnh nó thực sự làm việc, vì vậy cảm ơn bạn. Tôi sẽ đăng binder tinh chỉnh của tôi. –

1

Bạn có thể phân tích cú pháp giá trị lên trước khi gọi một trong các phương pháp này không? Nếu có, bạn có thể sử dụng phương pháp sau để làm như vậy

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

Điều này tôi nghĩ sẽ tuyệt vời như một người trợ giúp html – griegs

+0

Việc phân tích cú pháp thường không phải là vấn đề, nhưng tôi có một số trường "tiền" và tôi không muốn phân tích các bộ điều khiển của mình phân tích xung quanh TryUpdateModel, nếu có thể . –

+0

@cadmium sử dụng chất kết dính mô hình tùy chỉnh, xem liên kết trong câu trả lời của tôi. – eglasius

2

trả lời đã được trao cho Freddy Rios kể từ khi liên kết của mình cung cấp cho tôi với các cơ sở để làm điều này, nhưng mã cần một số sửa chữa lên:

// 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; 
    } 
} 

Nó không phải là đẹp, nhưng nó công trinh.

Các vấn đề liên quan