We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Enumeration in model and filtering

Currently got an issue which i cant figure out an easy way to solve. With a enum in my model, i would like to make it sortable/filterable in the grid however when data is typed in (its server sorting/filtering) it throws exception, since the query/enum is a number in the database. I use datamanager and dataoperations to make the query

I dont particular use the helpers in views, however i am using aspnet mvc.

Controller odata
        public ActionResult Logs(DataManager dm)
        {
            DataResult result = new DataResult();
            DataOperations opt = new DataOperations();
            var datasource = db.Logs;
            var dataResult = opt.Execute(db.Logs, dm);

            var list = dataResult.Cast<Core.DomainModels.Log>();
            var viewModelList = list.Select(x => new LogGridViewModel(x));

            return Json(new DataResult() { result = viewModelList, count = datasource.Count() });
        }

View grid
        $("#Grid").ejGrid({
            cssClass: "log-grid",
            dataSource: dataManager,
            allowScrolling: true,
            scrollSettings: { height: 800, allowVirtualScrolling: true, virtualScrollMode: ej.Grid.VirtualScrollMode.Normal },
            pageSettings: { pageSize: 50 },
            allowSorting: true,
            allowResizeToFit:true,
            enableRowHover: false,
            allowFiltering: true,
            sortSettings: { sortedColumns: [{ field: "Date", direction: ej.sortOrder.Descending }] },
            columns: [
                        { field: "Date", headerText: "Date", format: "{0:@System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern}" },
                        { field: "ShiftString", headerText: "Shift" },
                        { field: "EquipmentNr", headerText: "EquipmentNr" },
                        { field: "OrderNr))", headerText: "OrderNr" },
                        { field: "SupervisorCalled))", headerText: "SupervisorCalled" },
                        { field: "Issue", headerText: "Issue" },
                        { field: "Repairs", headerText: "Repairs" },
                        { field: "Responsible", headerText: "Responsible" },
                        { field: "LevelString", headerText: "Level" },
                        {

                            templateID: "#columnTemplate",
                            width: 50,
                            textAlign: ej.TextAlign.Center,
                        }
            ],
            }
        });

Model
    public class LogGridViewModel
    {
        public int Id { get; set; }
        public string ShiftString { get { return this.MemberDisplayNameFromValue(x => x.Shift); } }
        public string LevelString { get { return this.MemberDisplayNameFromValue(x => x.Level); } }
        public DateTime Date { get; set; }
        public Shifts Shift { get; set; }
        public int EquipmentNr { get; set; }
        public int OrderNr { get; set; }
        public bool SupervisorCalled { get; set; }
        public string Issue { get; set; }
        public string Repairs { get; set; }
        public string Responsible { get; set; }
        public Levels Level { get; set; }
    }


So in short its just how to make the grid and odata(with entity framework) play nice with enums.

Thanks to anyone who can explain me it.

5 Replies

SR Sellappandi Ramu Syncfusion Team October 5, 2015 10:36 AM UTC

Hi Quottrup,

Thanks for contacting Syncfusion support.

We have created sample with Enum type but we are unable to reproduce the issue. Please refer to the following sample.

Sample: 
MVCEnumType

Please share the following details to sort out the cause of the issue and provide a prompt solution.


1. In your controller code you have returned viewModelList. Please check whether you have returned correct list of records. Because we need to return the Execute() results.

2. Share the version of Essential studio.

3. Share the screen shot of the exception message to us.

4. Provide the issue reproducing sample, if it is possible. It will help us to provide the better solution sooner.

Regards, 
Sellappandi R.



BL Blom October 5, 2015 05:54 PM UTC

Unfortunately that example doesnt deal with the enum as a text in the grid. Atleast when im testing it.

Is it possible to get the enum text out and searchable with the grid and data operations?


SR Sellappandi Ramu Syncfusion Team October 6, 2015 02:39 PM UTC

Hi Quottrup,

By default Enum value will be displayed while using Enum type property. If we want to display enum text we need to serialize the enum value as string format.


This is not supported for javascript serializer to convert enum to string so we have provided workaround using Newtonsoft serialization to serialize the data and display in grid column.

Please refer to the following code example,

        public static List<Details> GetInversedData()

        {

            List<Details> obj = new List<Details>();

            for (var i = 0; i < 1; i++)

            {

                ….

            }                                                                            

            return obj;

        }

        protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)

        {

            return new JsonNetResult

            {

                Data = data,

                ContentType = contentType,

                ContentEncoding = contentEncoding,

                JsonRequestBehavior = behavior

            };

        }

    }

    public class Details

    {

        ….

        [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]

        public City Country { get; set; }

    }

    public class JsonNetResult : JsonResult

    {

        public JsonNetResult()

        {

            Settings = new JsonSerializerSettings

            {

                ReferenceLoopHandling = ReferenceLoopHandling.Error

            };

        }


        public JsonSerializerSettings Settings { get; private set; }


        public override void ExecuteResult(ControllerContext context)

        {

            if (context == null)

                throw new ArgumentNullException("context");


            HttpResponseBase response = context.HttpContext.Response;


            if (this.ContentEncoding != null)

                response.ContentEncoding = this.ContentEncoding;

            if (this.Data == null)

                return;


            response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;

            new Newtonsoft.Json.JsonSerializer().Serialize(response.Output, this.Data);

        }

    }


}


While filtering enum text, We need to type cast value as Enum and the filter will support the equal operator alone in enum column.

Please refer to the following code example for type casting,

public ActionResult Logs(DataManager dm)

        {

            if (dm.Where != null && dm.Where.Count != 0)

            {

                for (var val = 0; val < dm.Where.Count; val++)

                {

                    if (dm.Where[val].Field == "Country")

                        dm.Where[val].value = (City)Enum.Parse(typeof(City), dm.Where[val].value.ToString(), true);

                }

            }

            DataResult result = new DataResult();

            DataOperations opt = new DataOperations();

            IEnumerable datasource = GetInversedData().ToList();

            var dataResult = opt.Execute(datasource, dm);

            return Json(new DataResult() { result = dataResult, count = datasource.AsQueryable().Count() });
        }


Please refer to the following sample,

Sample: 
MVCEnumType

Regards,
Sellappandi R


BL Blom October 6, 2015 08:29 PM UTC

Well seing that sample, i guess the more correct way would be to just implement a dropdown menu with the enums as the filter in the grid?


SR Sellappandi Ramu Syncfusion Team October 7, 2015 07:18 AM UTC

Hi Quottrup,

We considered this requirement “DropDown list instead of text box in filter bar” as feature and a support incident has been created under your account to track the status of this requirement. Please log on to our support website to check for further updates.

https://www.syncfusion.com/account/login?ReturnUrl=/support/directtrac/incidents

Regards,
Sellappandi R

Loader.
Live Chat Icon For mobile
Up arrow icon