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() }); |