Search code examples
c#asp.net-mvckendo-gridkendo-asp.net-mvctelerik-grid

Nullable Enum property in kendo Grid


I have couple of properties that are enums and I want to bind them to grid. I am using following code

column.ForeignKey(p => p.<EnumpropertyName>, 
Model.<EnumList_As_SelectedItemList>, "Value", "Text");

EnumpropertyName is nullable Enum.

In another column, I refer to child property of property

column.ForeignKey(p => p.<Enumproperty2Name>.<childProperyName>, 
Model.<AnotherEnumList_As_SelectedItemList>, "Value", "Text");

childProperyName is nullable Enum.

As this is inside child object I have

.Model(model =>
 {
    model.Id(p => p.InfoTableId);
    model.Field(p => p.<Enumproperty2Name>).DefaultValue(new      Enumproperty2Name());
  })

This is not working, as I add new row and select value for these dropdown, value is lost after selection. However if I remove nullable and let them be regular enum properties, they work.

How to make nullable enums work in kendo grid.

Thanks


Solution

  • To add a dropdown inside a Kendo Grid try the following.

    columns.ForeignKey(p => p.ExamDateStatus, (System.Collections.IEnumerable)ViewData["ExamStatus"], "Value", "Text")
    .Title("Status").EditorTemplateName("ComboForeignKey").Width(100); 
    

    "ComboForeignKey" is a partial view which resides Views\Shared\EditorTemplates folder. It's content must be something similar to following.

    @model object
    
    @(
     Html.Kendo().DropDownListFor(m => m).OptionLabel("Select Below...")
     .HtmlAttributes(new { data_value_primitive = "true"})
     .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
    )