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

PercentTextBox + [DisplayFormat(DataFormatString = "{0:P}")]

4 Answers 474 Views
NumericTextBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
balticer
Top achievements
Rank 1
balticer asked on 21 Mar 2011, 03:17 PM
Hi,

example:

public class TaxRate
{
  public long Id { get; set; }
 
  [DisplayName("Titel")]
  [Required()]
  public string Title { get; set; }
 
  [DisplayName("Wert")]
  [Required()]
  [DisplayFormat(DataFormatString = "{0:P}")]
  public double Value { get; set; }
}

following the "Value" is correctly formatted in the grid as percent. If Value in the database = 0.15, then in the grid it is shown as 15 %. Right!

If i use the PercentTextBox to input the value, the value in the model is the value which is shown in the textbox - and that's the problem. If the PercentTextBox shows "15 %", then on server side the model holds 15 - which is not correct, it schould be 0.15.

Any suggestion for this problem?

regards
Kai

4 Answers, 1 is accepted

Sort by
0
tom
Top achievements
Rank 1
answered on 23 Aug 2011, 11:36 PM
We also have this very annoying problem which nearly renders this control useless!

To me it seems that this has not been properly considered. It is very very common for percentages to be stored in the database as floats and usually therefore as a ratio of 1 (for ease of calculations etc).

What we have had to do is create wrapper Properties around every percentage we want the user to edit (quite a few!) that multiply by 100 in the getter and divide by 100 in the setter. We then need 2 types of Display templates, one for non-editable properties that formats (as you have by 100) and one for editable properties that must not be multiplied by 100. It is a nightmare and I strongly suggest that Telerik consider adding a ValueIsRatio property that would tell the control to multiply values by 100 when binding and divide by 100 when saving values back.

I am strongly considering writing a custom version of the PercentageTextbox that would bind to an OnSave (when user finishes editing) and divides the value by 100 before updating the underlying values collection.

Telerik please add this feature!

ps. please also add data-validation-range attributes when the Min and Max properties are set and do not prevent the control even rendering when the database data exceeds these, this is madness and caught us out big time. Max and Min should very clearly be for input validation not binding validation!
0
Georgi Krustev
Telerik team
answered on 24 Aug 2011, 01:08 PM
Hello Tom,

 
Here is a quote of the answer to the support ticket open on the same matter:

This is a known issue. I am glad to inform you that we will introduce property in the PercentTextBox in order to control value ratios.

As a workaround you can override format method of the percent textbox and thus to achieve the required functionality:

Copy Code
<%= Html.Telerik().PercentTextBox()
                  .Name("PercentTextBox")
                  .Spinners(Model.PercentShowSpinners.Value)
                  .MinValue(Model.PercentMinValue.Value)
                  .MaxValue(Model.PercentMaxValue.Value)
                  .ClientEvents(e => e.OnLoad("load"))
 %>
 
 <script type="text/javascript">
     function load() {
         var textbox = $(this).data("tTextBox");
         textbox.format = $.proxy(function (value) {
                      return $.telerik.formatNumber(value,
                                   this.numFormat,
                                   this.digits,
                                   this.separator,
                                   this.groupSeparator,
                                   this.groupSize,
                                   this.positive,
                                   this.negative,
                                   this.symbol,
                                   false);
                 }, textbox);
     }
 </script>


Regards,

Georgi Krustev
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Raemil
Top achievements
Rank 2
answered on 31 Oct 2012, 12:34 PM
Georgi Krustev:"Here is a quote of the answer to the support ticket open on the same matter:

This is a known issue. I am glad to inform you that we will introduce property in the PercentTextBox in order to control value ratios."



I am having this same issue with "PercentTextBoxFor", as well.  

I have tried both extensions and was wondering which release this new property will be added.  I am currently using 2012.3.1018.340 and it still has a 1:1 ratio rather than the preferred 1:100 ratio.  I see no members of these extensions that would allow me to change that?

Also, since this thread is over a year old, is there a better method for translating the database value of (as an example) "0.0425" to a displayed/editable value of "4.25 %", and storing, if editing, in the database as "0.0425"?

Thanks,
Jerome.

0
Georgi Krustev
Telerik team
answered on 05 Nov 2012, 08:39 AM
Hello Jerome,

 
We did not introduce the aforementioned property as there was not enough requirements for it. For now you can use the suggested approach for multiplying the value of the input component. On the server you will need to divide the sent number by 100. 

As a side note, the Kendo UI Complete for ASP.NET MVC has this functionality built-in.

Regards,
Georgi Krustev
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
Tags
NumericTextBox
Asked by
balticer
Top achievements
Rank 1
Answers by
tom
Top achievements
Rank 1
Georgi Krustev
Telerik team
Raemil
Top achievements
Rank 2
Share this question
or