Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Posting comma as decimal separator to AppService #6808


User avatar
0
p.j.keukens created

Hi I have a question,

I have a form in a modal and there are input fields with numeric values. In Holland we use a comma as decimal separator like 23,34. I transform the form to an object using serializeFormToObject(); The result that's get posted to the appservice is:

{"Id":"9644f5f2-6146-404e-c629-08d6bc1eb906","PremiumBase":"12,23"}

Then the appservice has a class as input where PremiumBase is defined as a decimal. But when look at the value of PremiumBase in AppService the value is 1223 so the comma is lost as it is probably seen as a group separator. How can I solve this?

Best regards,

Patrick


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @p.j.keukens

    What is the selected language on the app, is it Dutch ?

    If you want to use a specific decimal seperator regardless of the selected culture, you can remove usage of app.UseAbpRequestLocalization(); in your Startup.cs and use a similar method like this one https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.AspNetCore/AspNetCore/AbpApplicationBuilderExtensions.cs#L86

    In this method, you need to set .NumberFormat.NumberDecimalSeparator = "."; for each culture in supportedCultures array.

  • User Avatar
    0
    p.j.keukens created

    Hi @ismcagdas,

    I've set the Abp.Localization.DefaultLanguageName setting for the host to nl. In the app I've removed all other languages and only left 'dutch' as a language, I've also removed the language selector for the users as everything should be in dutch only.

    I tried removing app.UseAbpRequestLocalization(); but that didn't make a difference, the same for setting UseAbpRequestLocalization = false to true both gave the same result.

    So why is this not working... am I missing something.. it should be working in asp.net zero right?

  • User Avatar
    0
    p.j.keukens created

    Hi @ismcagdas,

    I've looked into this a bit more but it seems that all settings are correct, so why isn't it working?

    First I've added the UseAbpRequestLocalization method to my startup class and looked at the available languages and cultures. There was one language available "nl" which is correct. Look at the screenshot below:

    The options underneath are send to app.UseRequestLocalization which all seem correct

    Then I go to my page which looks like this:

    The input for "Premie-voet" is of type text and an input mask is used then on save the information is send to app service using javascript: var setting = _$updateSettingForm.serializeFormToObject(); _insuranceService.updateInsuranceSetting(setting ).

    When I look at the value that gets posted it's a string value like "8,75".

    Before the appservice there is a customvalidator when I look at the current culture using System.Threading.Thread.CurrentThread.CurrentCulture the default culture is "nl" and the decimal separator is , which is correct as we use a , in holland as decimal separator. But the value of the field 'premie-voet' is 875 instead of 8,75. See the image below:

    So I'm baffled why it's not working properly.

    Please help me to get this sorted asap because it needs to work properly for a production system.

    Cheers Patrick

  • User Avatar
    0
    p.j.keukens created

    Hi @ismcagdas,

    I think I've solved this one. As the data is send as JSON the .net core mvc app needs to know which culture should be used to parse the json. I've added the last line so that json number string values are properly converted to numbers.

     //MVC
                services.AddMvc(options =>
                {
                    options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
                }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
                  .AddJsonOptions(options => options.SerializerSettings.Culture = CultureInfo.CurrentCulture);
    

    So this seemed to solve my problem: .AddJsonOptions(options => options.SerializerSettings.Culture = CultureInfo.CurrentCulture);

    Might be good to add that to aspnet zero as well...

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks @p.j.keukens :)