- Home
- Forum
- ASP.NET MVC
- Filtering/Sorting/Searching not working if grid is configured with CRUD and UrlAdaptor
Filtering/Sorting/Searching not working if grid is configured with CRUD and UrlAdaptor
Thanks for using Syncfusion products.
When using remote data binding in grid, the remote service should be configured to handle any grid actions such as filter, sort, paging etc. In your code, the controller`s action which returns grid data is not configured to handle any grid actions and hence the sorting and filtering were not worked.
|
public ActionResult DataSource(DataManager dm) { //SIMPLY RETURNING take AND skip values, not handled other grid actions dataresult.result = mylist.Skip(dm.skip).Take(dm.take); dataresult.count = mylist.Count();
return Json(dataResult, JsonRequestBehavior.AllowGet); |
To resolve this problem, we suggest you to return the data based on the grid action information which will be obtained in DataManager dm argument.
And you have mentioned as “is there an embedded way to "convert" this info in the datamanager into a NHibernate query?” in your query, we don’t have in-built way to convert the DataManager information to NHibernate query, but we can achieve this with the following steps.
1. Create a method to build NHibernate query from DataManager information. The ICriteria is used in the below code example.
|
private ICriteria ToQuery(DataManager dm, ISession session) {
ICriteria criteria = session.CreateCriteria<EmployeeMap>(); Count = session.CreateCriteria<EmployeeMap>().List().Count; Order order = null; if (dm.Where != null && dm.Where.Count > 0) //Filtering { for (var i = 0; i < dm.Where.Count; i++) {
WhereFilter filter = dm.Where[i]; if (filter.IsComplex) WhereFilter(criteria, filter); //Handle multiple column filter – REFER BELOW SAMPLE FOR CODE if (filter.Operator == "equal") criteria.Add(Restrictions.Eq(filter.Field, filter.value)); if (filter.Operator == "notequal") criteria.Add(Restrictions.Not(Restrictions.Eq(filter.Field, filter.value))); if (filter.Operator == "greaterthan") criteria.Add(Restrictions.Ge(filter.Field, filter.value)); if (filter.Operator == "lessthan") criteria.Add(Restrictions.Lt(filter.Field, filter.value)); if (filter.Operator == "greaterthanorequal") criteria.Add(Restrictions.Ge(filter.Field, filter.value)); if (filter.Operator == "lessthanorequal") criteria.Add(Restrictions.Le(filter.Field, filter.value)); if (filter.Operator == "startswith") criteria.Add(Restrictions.Like(filter.Field, filter.value + "%")); if (filter.Operator == "endswith") criteria.Add(Restrictions.Like(filter.Field, "%" + filter.value)); if (filter.Operator == "contains") criteria.Add(Restrictions.Like(filter.Field, filter.value.ToString(), MatchMode.Anywhere)); Count = criteria.List().Count; //Required to show filter count } } if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting { for (var i = 0; i < dm.Sorted.Count; i++) { order = dm.Sorted[i].Direction == "ascending" ? Order.Asc(dm.Sorted[i].Name) : Order.Desc(dm.Sorted[i].Name); criteria.AddOrder(order); } } if (dm.Skip != 0) //Paging { criteria.SetFirstResult(dm.Skip); } if (dm.Take != 0) //Paging { criteria.SetMaxResults(dm.Take); } return criteria; |
2. Now the above method can be used in the action which returns grid data as follows.
|
//PROVIDE GRID DATASOURCE public ActionResult GetData(DataManager dm) { using (ISession session = AppSession.OpenSession()) { list = ToQuery(dm,session).List<EmployeeMap>(); } return Json(new { result = list, count = Count }, JsonRequestBehavior.AllowGet); |
For your convenience we have also created a simple grid sample with above code example and the same can be downloaded from the below location.
Sample Location: http://www.syncfusion.com/downloads/support/forum/120120/ze/UsingNHibernate_-_QueryFromDataManager110626930
In the above sample, the DataManager is used to build NHiberate query which will be used to retrieve appropriate data on grid action.The sample is created using Essential Studio v13.2.0.34.
Please refer the below link for available APIs in DataManager class.
http://www.syncfusion.com/kb/4300/server-side-api-for-datamanager-operations
Please let us know if you have any query.
Regards,
Madhu Sudhanan. P
Thanks for the appreciation. We are happy that the requirement achieved.
Regards,
Madhu Sudhanan. P
Good day
I am new to blazor and syncfusion,
I can use your ToQuery method code and it will work very well I think, however where does ICriteria come from?
I have an Interface ISession as I'm using the WebApiAdaptor but I don't have an interface called ICriteria.
I am using Visual Studio 2022.
Please help as I would like to use your code.
Hi Jessica,
Before we start proceeding with your query, we request you to share with us the details below to provide you with the solution as early as possible.
- Confirm whether you are using the DataGrid or Treegrid component.
- Detailed explanation of your requirement and share video demo of your requirement.
- Complete code example.
Regards,
Pon selva
- 7 Replies
- 4 Participants
-
CA Carlo
- Sep 2, 2015 07:26 AM UTC
- Dec 6, 2023 12:59 PM UTC