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

[Solved] Grid localization in partial views

4 Answers 59 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Hartmut Kocher
Top achievements
Rank 1
Hartmut Kocher asked on 10 Nov 2011, 12:42 AM
Hi,
I am using the grid in dynamically (AJAX) loaded views. This works fine until globalization comes into play...

1) ScriptRegistrar won't output globalization information
When you put a Telerik component directly on the view, @Html.Telerik().ScriptRegistrar().Globalization(true) (which is part of my _Layout.cshtml) will output culture specific information in a CDATA block.
However, because my main view has just some HTML skeleton and the real content is loaded via AJAX, the ScriptRegistrar will output nothing! While the grid itself will automatically load the needed JavaScript files in the partial view, the culture info is missing. Therefore, numbers and dates are formatted incorrectly.

I worked around this by adding the JavaScript-files to the ScriptRegistrar and forcing it to output the CDATA block with the culture info. The following snippet does the job for me:
@Html.Telerik().ScriptRegistrar().Globalization(true).DefaultGroup(a =>
    {
a.Add(
"~/Scripts/2011.2.914/telerik.common.min.js");
a.Add(
"~/Scripts/2011.2.914/telerik.grid.min.js");
a.Add(
"~/Scripts/2011.2.914/telerik.textbox.min.js");
a.Add(
"~/Scripts/2011.2.914/telerik.grid.editing.min.js");
   }).OnDocumentReady(()=>{}))
Note the OnDocumentReady-Funktion which actually triggers the output of the CDATA block!

Now, numbers are formatted correctly, even after sorting and paging with AJAX.
BTW: This took my almost 2 days to track down and find the above workaround.

However, I still have a problem: When I edit a row (let's say in German culture) the number "2,5" switches to "2.5". Of course this leads to validation problems. If I manually change the "." back to "," everything works as expected (And yes, I use Globalize.js to localize JQuery validation, works great!).

Any ideas why this display uses the correct formatting vs. editing uses the default formatting?


Regards Hartmut
   

4 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 10 Nov 2011, 10:19 AM
Hello Hartmut Kocher,

 I tried to reproduce the described problem to no avail. Please find attached my test project.

Regards,
Atanas Korchev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Hartmut Kocher
Top achievements
Rank 1
answered on 10 Nov 2011, 11:24 AM
Hello Atanas Korchev,
thanks for your example.
I modified your project so that it has the described behavior.

1.) Missing culture info
In your project you included the Telerik Menu component in the layout file. Therefore, the ScriptRegistrar emits the culture info. I have commented the menu. Now the ScriptRegistrar emits nothing because no Telerik component is on the main page.

There should be an option to ScriptRegistrar.Globalize(true) to force emitting the culture info (or it should do so by default).

2.) Localization info not used by default
For the following to work, you first have to first uncomment the menu component in the _layout file, so that culture info is again emitted.

Then i did the following: I forced the page to be "de-DE" culture (see Index.cshtml).
Then I changed the grid definition to include the price column twice:
One version has a default definition, the other has a Format("{0:0.00}") definition.
If you now look at the grid you will see the only the formatted column has the correct number format (e.g. "2,45). So you are forced to specify a number format string to get localization!

3.) Edit doesn't respect localization information
If you now press the edit button, you will notice the number switches from "2,45" to "2.45". If you'd submit this to the server you'll get an validation error...

Hope this helps.
Kind regards
Hartmut

0
Accepted
Atanas Korchev
Telerik team
answered on 10 Nov 2011, 12:51 PM
Hi Hartmut Kocher,

 Thank you for the cooperation. I see where the problem is. Indeed the Globalization should be output without the need of an empty OnDocumentReady statement. We will fix that in the upcoming release.

 The problem with the text box showing 2.5 instead of 2,5 however cannot be solved easily. The grid is using JavaScript to set the value of the textbox and passes a JavaScript Number object which is then converted by the <input> element to a string without taking the current culture into account. A possible solution is to use a different editor template with a component which supports cultures such as the CurrencyTextBox or NumericTextBox. 

 I have modified the project to use the CurrencyTextBox and it should work now.

Regards,
Atanas Korchev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Hartmut Kocher
Top achievements
Rank 1
answered on 10 Nov 2011, 01:49 PM
Thank you. I am looking forward to the new release.

However, in the long run, I think you should address my issues 2 and 3 directly in the grid component by either using your own input component for numbers or by converting the number first before setting it as a string in the input field. Using templates for this is only a temporary workaround IMHO. Also it should not be necessary to add a number format specification to get a localized number...

Regards
Hartmut
Tags
Grid
Asked by
Hartmut Kocher
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Hartmut Kocher
Top achievements
Rank 1
Share this question
or