Hi,
I was told to ask here. Enums seem not to be supported by kendo's datasource in conjunction with OData v4 and MVC.
E.g. if I have a nullable enum property on an entity, the following issues arise:
1) The grid won't display values for such types.
2) When editing or adding new records, the datasource becomes confused and sends always the value null to the server.
Workaround:
(told me by one of Telerik's supporters today)
a) Define as foreign key to a list of enum-name/display: E.g.: columns.ForeignKey(m => m.Gender, PickItemsHelper.ToSelectList<PersonGender>(includeNull: true, useNames: true));
b) Define as of type string in the schema -> model : model.Field("Gender", typeof(string));
This now works for (1) and (2). I consider this a temporary workaround for a non-implemented part of the OData v4 spec.
However, when applying the workaround an additional issue pops up:
3) When explicitly selecting the null value (i.e. when we have a nullable enum), the datasource now sends an empty string to the server, which rejects the request as invalid because it expects either an enum name or null.
Any clues how to work around issue (3)?
Best regards,
Kasimier Buchcik
17 Answers, 1 is accepted

Additionally a fourth issue arises:
4) When the nullable enum's value is null initially and then one tries to edit that value, then any value selection results in the value "[object Object]" being sent by the datasource to the server and rejected. This is similar what happens with nullable GUIDs. But in this case one can not specify a default value as a workaround.
Two blockers here.
Best regards,
Kasimier Buchcik

I found a workaround for issue (4) here: http://www.telerik.com/forums/dropdownlistfor-return-value-of-object-object-
One has to use ValuePrimitive(true) on the DropDownList.
Can't find a solution for issue (3). Kendo still sends an empty string when adding a new record.
I'll try to describe the scenario:
1) Use an entity with a property of type nullable enum
2) Use Web API 2.2, OData v4
3) Use a kendo grid
4) Use a pop up editor template with a DropDownList, use ValuePrimitive(true) and bind to e.g. list of:
{ null, "" }, {"One", "One"}, {"Two", "Two" }
5) Add a new record via the grid. *Don't* change the selection of the enum property (i.e. leave it null)
6) Apply changes
7) Via Fiddler is see that an empty string was sent to the server
7) Server error: "Es müssen gültige Informationen angegeben werden, um die Zeichenfolge einzufügen."
Or english: "Must specify valid information for parsing in the string."
Any help here?
Best regards,
Kasimier Buchcik


I now solved my issue (3) by setting all relevant property values to null in the "edit" event of the grid. Apparently specifying default null values via the html helper is not honored, so one has to set those manually.
By the way: I learned that my issues here are related to all nullable types and are not specific to enums.
Remaining issue: The filters still show the [object Object] entry. Is there an option (like ValuePrimitive(true) for the DropDownList) to let the grid know that it is using a primitive data type for a column?
Feature request:
In a nice world the grid itself could and would inspect the given model type (i.e. via Html.Kendo().Grid<Person>()) and weave the necessary settings for nullable property types. Any plans to automate this for us?
Best regards,
Kasimier Buchcik
I'm afraid that setting { null, "" } as a possible filter value is not supported. You should either remove it completely or try using something like { "", " " } or { null, " " } (note the space in the text) instead.
Regards,
Rosen
Telerik

Hi Rosen,
Using { null, " " } as you proposed indeed fixes the display of the filter items.
Next issues:
1) Unfortunately selecting the null item from the list and then clicking of "filter" does nothing - kendo doesn't filter.
2) Filtering of OData enums doesn't work for me on the ASP.NET side.
See http://forums.asp.net/t/2048750.aspx?Web+API+OData+v4+exception+when+using+enum+filter
Rosen, does filtering of enums work on your side using OData v4?
Best regards & thanks,
Kasimier Buchcik


The null value in the items will match option label of the DropDownList value. This is due to fact that the option label is design to be shown when there is no value (a null).
The actual filtering will depend on how the server will handle the equals filter on a null value ($filter=Gender+eq+null), as in this scenario such filter expression will be created (see bellow for my OData v4 findings). Please note the DataSource itself those not provide a IsNull/NotIsNull filter operators. Here is the uservoice entry for adding such support.
Regarding, the OData v4. It seems that it will require spcial handling for filtering on Enums. It is similar to the following:
$filter=MyEnumProp has MyWebApplication.Models.MyExampleEnum'One'
This is not currently supported by our implementation. Note that the DataSource does not have knowledge if given value represents a .NET enum type nor what is the actual server enum type name.
Regards,
Rosen
Telerik

Hi Rosen,
Ok, I voted on uservoice.
The "has" operator would only be needed if using [Flags] enums. (http://docs.oasis-open.org/odata/odata/v4.0/odata-v4.0-part2-url-conventions.html). This means, in my case this should work out of the box. Something's wrong on ASP's side.
I opened an issue at https://github.com/OData/odata.net/issues/182
Best regards,
Kasimier Buchcik

Hello Kasimier,
Great, let us know if there is any progress on the issue on OData side.
Regards,Rosen
Telerik

Rosen,
Support for prefix free nullable enum types was not implemented yet in System.Web.OData for this specific scenario. For now I applied the fix locally on my side. Filtering works fine now. And yes, "xyz eq null" works as well.
See https://github.com/OData/WebApi/issues/331
Regards,
Kasimier Buchcik

Rosen,
you can close this thread if you want. With all the mentioned workarounds and fixes applied, I can now use enums with Kendo and OData v4.
Regards & thanks,
Kasimier Buchcik

I added a uservoice entry for the support of the "has" operator.
http://kendoui-feedback.telerik.com/forums/127393-telerik-kendo-ui-feedback/suggestions/7878327-datasource-odata-v4-support-has-operator

Rosen,
the Web API OData fix for nullable enums in filters in now available via the nightly build package:
<package id="Microsoft.AspNet.OData" version="5.6.0-beta1-150511-Nightly" targetFramework="net45" />
Hello Casimodo,
This sounds great, thanks for the information.
Regards,Rosen
Telerik