2

My current model to store a specific vendors currency is show below where the VendorCurrency value will be like "en-US" or "en-CA" and so forth.

Currently I am using the tag in the Web.Config which sets the default currency globally but the requirement to make the site multi-currency has come about.

Is there a simple way to use the model to determine which currency to use on the fly in Views?

Vendor:

public string VendorID { get; set; }

[Required]
[StringLength(100)]
[Display(Name = "Vendor Name")]
public string VendorName { get; set; }

[StringLength(50)]
public string VendorCurrency { get; set; }

Invoice:

public string Invoice_Number { get; set; }

public int VendorID {get;set;}

[DataType(DataType.Currency)]
public decimal? Invoice_Amount { get; set; }
user2806570
  • 821
  • 2
  • 12
  • 25
  • Are you wanting this for display only, or are you also wanting to edit the value in the users culture as well (in which case you would need to a custom ModelBinder and you would need to configure the `$.validator` if you also want client side validation) –  Apr 03 '18 at 06:29
  • This is for display only. Users wont be able to edit. – user2806570 Apr 03 '18 at 06:33
  • If that is the case, one solution would be to have a (say) `string CurrencyFormat` property which you could set in the GET method based on the users culture - for example it might be "$#.00" for `en-US` and then in the view use `@Model.Invoice_Amount.ToString(Model.CurrencyFormat)`. –  Apr 03 '18 at 06:40
  • Thanks! I sort of combined your answer and Cory's below to create an extension method to determine the Culture detected. – user2806570 Apr 03 '18 at 12:14

1 Answers1

1

You need to set Thread.CurrentThread.CurrentCulture in your controller, prior to returning the view:

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-Us");

Huge Caveat:

This setting will last beyond your view execution and action execution. It will bleed into future action executions.

Also, a lot of coders do not properly specify cultures when parsing/printing numbers and if you have code like that, setting CurrentCulture like this will subtly wreak havoc on it.

Cory Nelson
  • 29,236
  • 5
  • 72
  • 110