template respecting DisplayFormat attribute

5 posts, 1 answers
  1. Armin
    Armin avatar
    8 posts
    Member since:
    Nov 2012

    Posted 04 Sep Link to this post

    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?

  2. Neli
    Admin
    Neli avatar
    28 posts

    Posted 05 Sep Link to this post

    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.
  3. Armin
    Armin avatar
    8 posts
    Member since:
    Nov 2012

    Posted 07 Sep in reply to Neli Link to this post

    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?

  4. Answer
    Neli
    Admin
    Neli avatar
    28 posts

    Posted 08 Sep Link to this post

    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.
  5. Armin
    Armin avatar
    8 posts
    Member since:
    Nov 2012

    Posted 11 Sep in reply to Neli Link to this post

    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

Back to Top