Setting Currency independent of culture

NOVATROOP77 256 Reputation points
2021-06-12T17:38:44.633+00:00

I am having a futher issue here. In asp.net 5 and mvc

I want to have a dropdown with currency the part of selection and identifying which works

[HttpPost]
public IActionResult SetCurrency(IFormCollection forms,string returnUrl)
{
    string storeLocale = forms["customerCurrency"].ToString();

    var record = _context.Currencies.Where(w => w.DisplayLocale == storeLocale).FirstOrDefault();

    var currentLanguageId = _context.Appsettings.Where(w => w.Key == Constants.FrontEndDefaultLanguageId).FirstOrDefault();
    if (record != null)
    {
        _config[Constants.FrontEndDefaultLanguageId] = record.Id.ToString();
        currentLanguageId.Value = record.Id.ToString();
        _context.SaveChanges();
        _toast.AddAlertToastMessage("Currecy changed to " + record.Name); 
    }           

    return LocalRedirect(returnUrl);
}

For my culture I am doing this which is control by the language dropdown

[HttpPost]
public IActionResult SetCulture(string culture, string returnUrl)
{
    var record = _context.Appsettings.Where(w => w.Key == Constants.FrontEndDefaultLanguageId).FirstOrDefault();
        if (culture == "en")
            record.Value = "1";

        if (culture == "fr")
            record.Value = "2";
        _context.SaveChangesAsync();
        _toast.AddSuccessToastMessage("Language changed to :" + record.Key);

        Response.Cookies.Append(
            CookieRequestCultureProvider.DefaultCookieName,
            CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
            new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
        );
        return LocalRedirect(returnUrl);
 }

I don't want SetCurrency to override what has been set in the cookie but i want it to set if it would use the euro pound symbol etc so I think I need Number Info but am not sure how I would set it in this case.

Buy this i mean the

£ or the Euro symbol at present another square symbol is showing up even though my culture is set to fr-fr for france

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,398 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,415 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,640 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yihui Sun-MSFT 801 Reputation points
    2021-06-15T10:18:11.043+00:00

    Hi @NOVATROOP77 ,

    I would rather this not be something that the user has to configure I want it to act like how shopify and most other sites would act without any user intervention in the currency bar the dropdown.

    Can I understand it as follows:

    • Do you want to change the Currency automatically in a different culture?

    If so, you can follow the link I provided earlier. When you change the culture, the Currency changes. You can also refer to the code I tested below.

    Startup

    public void ConfigureServices(IServiceCollection services)  
                {  
                   ... ...  
                    services.AddLocalization(options => options.ResourcesPath = "Resources");  
                    services.Configure<RequestLocalizationOptions>(options =>  
                    {  
                        var supportedCultures = new[]  
                        {  
                            new CultureInfo("en-US"),  
                            new CultureInfo("fr")  
                         };  
                        options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");  
                        options.SupportedCultures = supportedCultures;  
                        options.SupportedUICultures = supportedCultures;  
                        options.AddInitialRequestCultureProvider(new CustomRequestCultureProvider(async context =>  
                        {  
                            // My custom request culture logic  
                            return new ProviderCultureResult("en");  
                        }));  
                    });  
                    services.AddMvc()  
                        .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)  
                        .AddDataAnnotationsLocalization();  
                }  
                // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
                public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
                {  
                    ... ...  
                    var supportedCultures = new[] { "en-US", "fr" };  
                    var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supportedCultures[0])  
                        .AddSupportedCultures(supportedCultures)  
                        .AddSupportedUICultures(supportedCultures);  
          
                    app.UseRequestLocalization(localizationOptions);  
          
                     ... ...  
                }  
    

    Controller

    public class CurrencyController : Controller  
            {  
                public IActionResult Index()  
                {  
                    return View();  
                }  
                [HttpPost]  
                public IActionResult SetLanguage(string culture, string returnUrl)  
                {  
                    Response.Cookies.Append(  
                        CookieRequestCultureProvider.DefaultCookieName,  
                        CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),  
                        new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }  
                    );  
          
                    return LocalRedirect(returnUrl);  
                }  
            }  
    

    View

    105802-77.gif


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Best Regards,
    YihuiSun

    0 comments No comments