This is a migrated thread and some comments may be shown as answers.

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

2 Answers 329 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Wim
Top achievements
Rank 1
Wim asked on 02 Jun 2015, 12:40 PM

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 Answers, 1 is accepted

Sort by
0
Wim
Top achievements
Rank 1
answered on 02 Jun 2015, 12:44 PM
[quote]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

 

[/quote]

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

0
Rosen
Telerik team
answered on 04 Jun 2015, 10:40 AM

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
Tags
Grid
Asked by
Wim
Top achievements
Rank 1
Answers by
Wim
Top achievements
Rank 1
Rosen
Telerik team
Share this question
or