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

Issue displaying Enum Description on Kendo grid

4 Answers 1707 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Guillermo
Top achievements
Rank 1
Guillermo asked on 13 May 2013, 09:23 PM
Hi Team,

We are trying to display Enum Description
for user friendly message. We have some issue on kendo grid template column.
How can we pass the data to the MvcHelperExtensions method.  Please
suggest if you have any solution for this.
columns:
[
          { field: "Status", title: "Status", template: kendo.template($("#tempStatus").html())}
]

<script id="tempStatus" type="text/x-kendo-tmpl">
<div>#= Status#</div>// We
are able to see the data
@*<div>@MvcHelperExtensions.GetWorkOrderStatusDescription("Status")</div>*@// We are not sure how to pass this data to a MvcHelperExtensions method.
 </script>

My Enum as as below.
 public enum WorkOrderStatus
    {
        [Description("Estimate")]
        Estimate = 0,
        [Description("Estimate sent")]
        SubmittedEstimate = 1,
}

My Extention method is below

public static string GetEnumDescription<TEnum>(TEnum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

            if ((attributes != null) && (attributes.Length > 0))
                return attributes[0].Description;
            else
                return value.ToString();
        }
 

4 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 15 May 2013, 04:05 PM
Hello Guillermo,

The template is executed on the client so a server-side helper cannot be used to get the correct text. What I can suggest in order to show the description, is to serialize the enumeration as JSON and set it to the column values e.g.

@{
    List<object> values = new List<object>();
 
    foreach (var value in Enum.GetValues(typeof(WorkOrderStatus)))
    {
        values.Add(new
        {
 
            text = GetEnumDescription(((WorkOrderStatus)value)),
            value = ((int)value).ToString()
        });
    }
 
}
...
 
<script>
    ...
     
    columns:
    [
              { field: "Status", title: "Status", values: @(Html.Raw(Json.Encode(values)))}
    ]
Kind regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Guillermo
Top achievements
Rank 1
answered on 16 May 2013, 11:12 PM
Dear Daniel,

Thanks for the reply. I tried as you have suggested it didn't work for me. My script file is in different location i have added like this below

columns: [
   { field: "Status", title: "Status", values: '@(Html.Raw(Json.Encode(values)))' }
        ]

It binds the enum value not it's description. The view code is fine it's getting all the enum and it's description but values: '@(Html.Raw(Json.Encode(values)))'  is not working. Please suggest what to do. If you have any sample please share with us.

Thanks
0
Petur Subev
Telerik team
answered on 20 May 2013, 01:32 PM
Hello Guillermo,

I am afraid that we do not have the exact same demo as in your scenario. However we have the following demos which demonstrate the usage of Enum:

http://www.kendoui.com/code-library/mvc/grid/ajax-editing-with-enumerations.aspx

http://www.kendoui.com/code-library/mvc/grid/radio-button-column-for-a-enum-field-in-batch-editing-.aspx

Feel free to modify any of them, replicate your case and send it back so we can see what needs to be troubleshooted.

Kind Regards,

Petur Subev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Masaab
Top achievements
Rank 1
answered on 29 Jan 2014, 11:59 PM
Hello 
Here is Quick Solution for Filtering on Enum.

<script type="text/javascript">
    function typeFilter(element)
    {
      @{
          var values = Enum.GetValues(typeof(MyType)).Cast<object>()
                           .Select(v => new SelectListItem
                           {
                               Selected = v.Equals(Model.MyLevel),
                               Text = v.ToString(),
                               Value = System.Convert.ToInt32(v).ToString()
                           });
      }
      

        var logValues = [
            @foreach(var selectListItem in values)
            {
                @:{
                        @Html.Raw(string.Format("Text: '{0}',", selectListItem.Text))
                        @Html.Raw(string.Format("Value: {0}", selectListItem.Value))
                 @:}, 
            }
        ];


        element.kendoDropDownList({
            dataSource: logValues,
            dataTextField: 'Text',
            dataValueField:'Value',
            optionLabel: "--Select My Level"
        });
       
    }
</script>
Tags
Grid
Asked by
Guillermo
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Guillermo
Top achievements
Rank 1
Petur Subev
Telerik team
Masaab
Top achievements
Rank 1
Share this question
or