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

[Solved] How to resolve enum string within Telerik ajax-bound client template?

1 Answer 90 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.
Brian Berman
Top achievements
Rank 1
Brian Berman asked on 16 Apr 2012, 07:43 PM

I am using an Ajax Binding on a Telerik MVC grid and I can't figure out how I would resolve the text version of an enum within the ClientTemplate.

Given:

enum MyEnum { Sat, Sun, Mon, Tue, Wed, Thu, Fri };

public class MyBusinessObject{
     
public int Id { get; set; }
     
public MyEnum Day { get; set;
}

In my view:

@(Html.Telerik().Grid<MyBusinessObject>()

... columns.Bound(o => o.Day) .ClientTemplate("????")

How can I resolve, for example, "Mon" within the column using the ClientTemplate?

1 Answer, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 17 Apr 2012, 06:52 AM
Hello Brian,

The problem with enumerations and Ajax binding is that the JavaScript serializer which serializes the data is serializing only the key of the enum property. (i.e. Sat, Sun etc. will become 0,1 ...) So I suggest you to use a ViewModel instead of binding your Grid to your actual model. This way you can create your Day property to be of type String instead of MyEnum.
Also to get the string representation of an enum property you can use the following approach:
Enum.GetName(typeof(MyEnum), m);
where m is the enum property of your model.
I hope this helps.

Regards,
Petur Subev
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.
Tags
Grid
Asked by
Brian Berman
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Share this question
or