How to perform Data Oeration in server side for Grid in ASP.NET MVC?
The DataManagerRequest class helps in binding the ASP.NET MVC Grid queries passed to the server-side. Based on those queries you can perform server-side operation on the Grid data.
The query parameters are listed below.
Table 1: Query Parameters
Expand | It is used as OData Expand query. |
RequiresCounts | When this property is set as True, the total count of the records wrapped as count property in the result. |
Skip | Page query to skip the records |
Take | Page query to get the required number of records |
Sorted | Sort Queries |
Table | It is a data source table name. |
Where | Filter queries |
Search | Search Queries |
DataManager Operations:
The query parameters are serialized by the DataManagerRequest class and the server-side operations such as sorting, filtering, paging are performed by the PerformSorting, PerformFiltering, PerformSkip and PerformTake methods.
Table 2: Order of execution recommended for Server-side operation
Order | Methods |
1 | PerformFiltering |
2 | PerformSearching |
3 | PerformSorting |
4 | PerformSelect |
5 | PerformSkip |
6 | PerformTake |
.Net Core
Follow the code example for the .Net Core EJ2 Grid.
Tag Helpers
<ejs-grid id="Grid" height="273" allowSorting="true" toolbar=toolbarItems allowPaging="true" allowFiltering="true"> <e-data-manager url="/Home/UrlDataSource" adaptor="UrlAdaptor"></e-data-manager> <e-grid-filterSettings type="Excel" ></e-grid-filterSettings> <e-grid-columns> <e-grid-column field="OrderId" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column> <e-grid-column field="CustomerId" headerText="Customer ID" type="string" width="120"></e-grid-column> <e-grid-column field="ShipName" headerText="ShipName" width="120"></e-grid-column> <e-grid-column field="EmployeeId" headerText="Employee ID" textAlign="Right" width="120"></e-grid-column> <e-grid-column field="ShipAddress" headerText="Ship Address" textAlign="Right" width="120"></e-grid-column> </e-grid-columns> </ejs-grid>
C#
public ActionResult UrlDatasource([FromBody]DataManagerRequest dm) { IQueryable<Orders> DataSource = db.Orders; QueryableOperation operation = new QueryableOperation(); if (dm.Where != null) { DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Condition); } if (dm.Search != null) { DataSource = operation.PerformSearching(DataSource, dm.Search); } int count = DataSource.Cast<object>().Count(); if (dm.Sorted != null) { DataSource = operation.PerformSorting(DataSource, dm.Sorted); } if (dm.Select != null) { DataSource = (IQueryable<Orders>)operation.PerformSelect(DataSource, dm.Select); } if (dm.Skip != 0) { DataSource = operation.PerformSkip(DataSource, dm.Skip); } if (dm.Take != 0) { DataSource = operation.PerformTake(DataSource, dm.Take); } return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource); }
MVC
Follow the code example of the Asp .Net MVC.
C#
public ActionResult UrlDatasource(DataManagerRequest dm) { IQueryable DataSource = db.Orders.AsQueryable().OrderBy("OrderID"); QueryableOperation operation = new QueryableOperation(); if (dm.Where != null) { DataSource = operation.PerformFiltering((IQueryable<Order>)DataSource, dm.Where, dm.Where[0].Condition); } if (dm.Search != null) { DataSource = operation.PerformSearching((IQueryable<Order>)DataSource, dm.Search); } int count = DataSource.Cast<Order>().Count(); if (dm.Sorted != null) { DataSource = operation.PerformSorting((IQueryable<Order>)DataSource, dm.Sorted); } if (dm.Skip != 0) { DataSource = operation.PerformSkip((IQueryable<Order>)DataSource, dm.Skip); //Paging } if (dm.Take != 0) { DataSource = operation.PerformTake((IQueryable<Order>)DataSource, dm.Take); } var countData = Json(new { result = DataSource, count = count }, JsonRequestBehavior.AllowGet); var orgData = Json(DataSource, JsonRequestBehavior.AllowGet); return dm.RequiresCounts ? countData : orgData; }
Razor
@(Html.EJS().Grid("Grid").Toolbar(toolbarItems).AllowFiltering() .AllowPaging() .FilterSettings(filter => { filter.Type(Syncfusion.EJ2.Grids.FilterType.Excel); }) .DataSource(dataManager => { dataManager.Url("/Home/UrlDatasource").Adaptor("UrlAdaptor"); }) .Toolbar(new List<string>() { "Search" }) .Columns(col => { col.Field("OrderID").HeaderText("Order ID").Add(); col.Field("CustomerID").HeaderText("Customer ID").Add(); col.Field("ShipName").HeaderText("Ship Name").Add(); col.Field("EmployeeID").HeaderText("Employee ID").Add(); col.Field("ShipAddress").HeaderText("Ship Address").Add(); }).Render())
Conclusion
I hope you enjoyed about how to perform data operation in server side for Grid ASP.NET MVC applcation.
You can refer to our ASP.NET MVC Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC Grid example to understand how to create and manipulate data.