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

template respecting DisplayFormat attribute

4 Answers 425 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Heli
Top achievements
Rank 2
Heli asked on 04 Sep 2017, 03:57 PM

Hi,

 

in a DDL I display entries of the following type:

public class ConfigEntryLine {
        public long ItemID { get; set; }
        public string ItemText { get; set; }
 
        [DisplayFormat(DataFormatString = "{0:c}")]
        public double ItemPrice { get; set; }

In the dropdown I want to display the"ItemPrice" on the right side formatted like given in the attribute on the model.

I use the DDL like in a editfor like this:

...
else {
    @(Html.Kendo().DropDownListFor(m => m.SelectedID)
                .DataTextField("ItemText")
                .DataValueField("ItemID")
                .AutoBind(true)
                .HtmlAttributes(new { style = "width:280px;font-size:small" })
                .ValuePrimitive(true)
                .BindTo(Model.Lines)
                 .Template("<span class=\"k-state-default\">#: data.ItemText#</span><span class=\"k-state-default pull-right\"><strong>#: data.ItemPrice#</strong>")
                );
}

I get the value - as number without decimal places and currency sign.

Is it possible to use the DisplayFormat attribute in a DDL template?

4 Answers, 1 is accepted

Sort by
0
Neli
Telerik team
answered on 05 Sep 2017, 10:29 AM
Hello Armin,

There is an easy way to display currency in desired format. By using Kendo toString method numbers and dates could be formatted depending on specified format and culture. 

In your case the Template could be as follows:

.Template("<span class=\"k-state-default\">#: data.ItemText#</span><span class=\"k-state-default pull-right\"><strong>#: kendo.toString(data.ItemPrice, 'c')#</strong>")


Or if a different than the default culture is needed, it should be set in the following way:
@(Html.Kendo().Culture("de-DE"))


and respectively for the data in the Template that have to be formatted:
<strong>#: kendo.toString(data.ItemPrice, 'c', 'de-DE')#</strong>


In case you have more question do not hesitate to contact us.


Regards,
Neli
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Heli
Top achievements
Rank 2
answered on 07 Sep 2017, 05:30 PM

Hi Neli,

 

my question was:

Is it possible to use the DisplayFormat attribute in a DDL template?

Your solutions show how to format it in a specific way. But NOT how to respect the DisplayFormat attribute from the model.
Or in other words - if I decide to change the DisplayFormat at the model a @Html.DisplayFor(a=>a.ItemPrice) would change the output too.

If this is not possible it would be OK to to format the data in a "user locale aware" fashion.

So (just for an example) - I want to display the string with a seperator and two digits.

Example: Germans should see 1,24 - US instead 1.24 if the value is 1.239

I already have set the "kendo locale" like this:

@* Include the JavaScript for the current culture *@
    @{ var culture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString(); }
    <script src="@Url.Content("~/Scripts/kendo/cultures/kendo.culture." + culture + ".min.js")"></script>
 
    @* Set the current culture *@
    <script>
    kendo.culture("@culture");
    

 

I WPF for an example I would set the format to 0.00 - which would work - depending on the current UI locale.

Is this possible with a DDL template too?

0
Accepted
Neli
Telerik team
answered on 08 Sep 2017, 02:45 PM
Hello Armin,

DisplayFormat is not supported in the DropDownList. I would suggest you to use the Kendo toString method as it was described in my first message. Currently, we are not aware of an alternative approach to the suggested way of formatting the values.

Please contact us in case you need further assistance.

Regards,
Neli
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Heli
Top achievements
Rank 2
answered on 11 Sep 2017, 08:26 AM

I just realized that Kendo toString respects the set locale.

So what I tried is not possible (respect server DisplayFormat like the builtin helpers). But at least I can get localized output.

Thank you

Tags
DropDownList
Asked by
Heli
Top achievements
Rank 2
Answers by
Neli
Telerik team
Heli
Top achievements
Rank 2
Share this question
or