- Home
- Forum
- ASP.NET MVC
- Enumeration in model and filtering
Enumeration in model and filtering
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.
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.
Is it possible to get the enum text out and searchable with the grid and data operations?
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
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
- 5 Replies
- 2 Participants
-
BL Blom
- Oct 4, 2015 07:29 PM UTC
- Oct 7, 2015 07:18 AM UTC