Add/Edit grid passes comma separated numeric value to ASP.NET

3 posts, 0 answers
  1. Wim
    Wim avatar
    16 posts
    Member since:
    Oct 2014

    Posted 02 Jun 2015 Link to this post

    I have a simple grid that uses a custom popup editor template. In the template, I'd like to use a Numeric Textbox.

    Due to localization, the application keeps trying to use a comma to separate decimals in the numeric values. When these values are passed to the ASP.NET MVC back-end, the value is lost and null is passed. How can I ensure the posted value has a period separator?

    I have tried setting the underlying field values to 2.5 instead of 2,5 in the grid's Save event, as well as tried to overwrite the e.model.WeightKg to 2.5. The value is still passed with a comma separator, as shown by inspecting the form data in the request.

     My grid:

    01.@(Html.Kendo().Grid<PackageViewModel>()
    02.    .Name("PackageGrid")
    03.    .Columns(columns => {
    04.        columns.Bound(o => o.PackageCode);
    05.        columns.Bound(o => o.WeightKg);
    06.        columns.Command(o => o.Edit());
    07.    })
    08.    .DataSource(d => d
    09.        .WebApi()
    10.        .Model(m => m.Id(o => o.PackageCode))
    11.        .Read(c => c.Url(Url.HttpRouteUrl("DefaultApi", new { controller = "Packages" })))
    12.        .Create(c => c.Url(Url.HttpRouteUrl("DefaultApi", new { controller = "Packages" })))
    13.        .Destroy(c => c.Url(Url.HttpRouteUrl("DefaultApi", new { controller = "Packages" id = "{0}" })))
    14.        .Update(c => c.Url(Url.HttpRouteUrl("DefaultApi", new { controller = "Packages" id = "{0}" })))
    15.        .PageSize(20)
    16.    )
    17.    .Editable(e => e.Mode(GridEditMode.PopUp).TemplateName("Package"))
    18.    .Selectable()
    19.    .Deferred()
    20.)

    The numeric textbox in the template:

    1.@Html.Kendo().NumericTextBoxFor(m => m.WeightKg).Decimals(8)

    And the unparsed form data, followed by the parsed form data:

    sort=&group=&filter=&PackageCode=DOOS-B&WeightKg=2%2C5
     
    sort:
    group:
    filter:
    PackageCode:DOOS-B
    LeegGewichtKg:2,5

  2. Wim
    Wim avatar
    16 posts
    Member since:
    Oct 2014

    Posted 02 Jun 2015 in reply to Wim Link to this post

    Wim said:

    And the unparsed form data, followed by the parsed form data:

    sort=&group=&filter=&PackageCode=DOOS-B&WeightKg=2%2C5
     
    sort:
    group:
    filter:
    PackageCode:DOOS-B
    LeegGewichtKg:2,5

     

    The last line is actually the correct field: WeightKg: 2,5. I forgot to translate part of the result.

  3. Rosen
    Admin
    Rosen avatar
    3253 posts

    Posted 04 Jun 2015 Link to this post

    Hello Wim,

    The behavior you have described is the way the DataSource handles the different culture formats for ASP.NET MVC ModelBinding. However, it does expect that the current thread culture will be the same as the UI and JavaScript culture.

    If you want to convert the numbers to different format, you could use the appropriate action's data function. For example here is how this can be done for Update method, where the UnitPrice is the field which should be converted:

    .Update(update => update.Url(/*..*/).Data("update"))
     
    <script>
        function update(data) {
            data.UnitPrice = data.UnitPrice.toString();
        }
    </script>

    Regards,
    Rosen
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top