|
<ejs-grid id="Grid" allowPaging="true" allowFiltering="true">
<e-data-manager url="/Home/UrlDataSource" adaptor="UrlAdaptor"></e-data-manager>
<e-grid-filterSettings type="Menu"></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="ShipCountry" headerText="Ship Country" width="150" ></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>
<e-grid-column field="Freight" width="150" ></e-grid-column>
</e-grid-columns>
</ejs-grid> |
|
public IActionResult UrlDatasource([FromBody]DataManagerRequest dm)
{
IEnumerable DataSource = order;
DataOperations operation = new DataOperations();
if (dm.Search != null && dm.Search.Count > 0)
{
DataSource = operation.PerformSearching(DataSource, dm.Search); //Search
}
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
DataSource = operation.PerformSorting(DataSource, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
int count = DataSource.Cast<Orders>().Count();
if (dm.Skip != 0)
{
DataSource = operation.PerformSkip(DataSource, dm.Skip); //Paging
}
if (dm.Take != 0)
{
DataSource = operation.PerformTake(DataSource, dm.Take);
}
return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource);
} |
|
OrderController.cs
public class OrderController : Controller
{
// GET: api/Orders
[HttpGet]
public object Get()
{
var queryString = Request.Query;
// perform the operation using the concern querystring
int skip = Convert.ToInt32(queryString["$skip"]); //paging
int take = Convert.ToInt32(queryString["$top"]);
string filter = queryString["$filter"]; // filtering
string sort = queryString["$orderby"]; // sorting
var data = OrdersDetails.GetAllRecords();
List<OrdersDetails> Datae = new List<OrdersDetails>();
List<OrdersDetails> Datase = new List<OrdersDetails>();
if (sort != null) //Sorting
{
switch (sort)
{
// here you can perform sorting action
case "OrderID":
if (sort.Substring(sort.IndexOf(' ') + 1) != null)
data = data.OrderByDescending(x => x.OrderID).ToList();
else
data = data.OrderBy(x => x.OrderID).ToList();
break;
case "CustomerID":
if (sort.Substring(sort.IndexOf(' ') + 1) != null)
data = data.OrderByDescending(x => x.CustomerID).ToList();
else
data = data.OrderBy(x => x.CustomerID).ToList();
break;
case "ShipCity":
if (sort.Substring(sort.IndexOf(' ') + 1) != null)
data = data.OrderByDescending(x => x.ShipCity).ToList();
else
data = data.OrderBy(x => x.ShipCity).ToList();
break;
case "ShipCountry":
if (sort.Substring(sort.IndexOf(' ') + 1) != null)
data = data.OrderByDescending(x => x.ShipCountry).ToList();
else
data = data.OrderBy(x => x.ShipCountry).ToList();
break;
}
}
if (filter != null)
{
// here you can perform filtering action
var newfiltersplits = filter;
var filtersplits = newfiltersplits.Split('(', ')', ' ');
var filterfield = filtersplits[1];
var filtervalue = filtersplits[3];
if (filtersplits.Length != 5)
{
filterfield = filter.Split('(', ')', '\'')[3];
filtervalue = filter.Split('(', ')', '\'')[5];
}
switch (filterfield)
{
case "OrderID":
Datae = (from cust in data
where cust.OrderID.ToString() == filtervalue.ToString()
select cust).ToList();
break;
case "EmployeeID":
Datae = (from cust in data
where cust.EmployeeID.ToString() == filtervalue.ToString()
select cust).ToList();
break;
case "Freight":
Datae = (from cust in data
where cust.Freight.ToString() == filtervalue.ToString()
select cust).ToList();
break;
case "CustomerID":
Datae = (from cust in data
where cust.CustomerID.ToLower().StartsWith(filtervalue.ToString())
select cust).ToList();
break;
case "ShipCity":
Datae = (from cust in data
where cust.ShipCity.ToLower().StartsWith(filtervalue.ToString())
select cust).ToList();
break;
case "ShipCountry":
Datae = (from cust in data
where cust.ShipCountry.ToLower().StartsWith(filtervalue.ToString())
select cust).ToList();
break;
}
Datase.AddRange(Datae);
// count = Datase.Count;
data = Datase;
}
// return the data with Items and Count pair
return new
{
Items = data.Skip(skip).Take(take),
Count = data.Count()
// return order;
};
}
// GET: api/Orders/5
[HttpGet("{id}", Name = "Get")]
public string Get(int id)
{
return "value";
}
// POST: api/Orders
[HttpPost]
public object Post([FromBody]OrdersDetails value)
{
OrdersDetails.GetAllRecords().Insert(0, value);
return value;
}
// PUT: api/Orders/5
[HttpPut]
public object Put(int id, [FromBody]OrdersDetails value)
{
var ord = value;
OrdersDetails val = OrdersDetails.GetAllRecords().Where(or => or.OrderID == ord.OrderID).FirstOrDefault();
val.OrderID = ord.OrderID;
val.EmployeeID = ord.EmployeeID;
val.CustomerID = ord.CustomerID;
val.Freight = ord.Freight;
val.OrderDate = ord.OrderDate;
val.ShipCity = ord.ShipCity;
return value;
}
// DELETE: api/ApiWithActions/5
[HttpDelete("{id:int}")]
[Route("Orders/{id:int}")]
public object Delete(int id)
{
OrdersDetails.GetAllRecords().Remove(OrdersDetails.GetAllRecords().Where(or => or.OrderID == id).FirstOrDefault());
return Json(id);
}
public class Data
{
public bool requiresCounts { get; set; }
public int skip { get; set; }
public int take { get; set; }
}
} |
|
<ejs-grid id="Grid">
<e-data-manager url="/api/Orders" adaptor="WebApiAdaptor" crossdomain="true"></e-data-manager>
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" type="number" textAlign="Right" width="120"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="140"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" width="120"></e-grid-column>
<e-grid-column field="OrderDate" headerText="Order Date" format='yMd' textAlign="Right" width="140"></e-grid-column>
</e-grid-columns>
</ejs-grid>
|
I have personally googled this issue extensively, (using this grid in a .NET 7 project) and i can say as from Feb 2023, the URLadapter works perfectly fine, the issue is the Nullreference occurs because the column names are serialised as camelCase, so you class might have a property called "EmployeeName", but the DataManagerRequest and associated DataOperations object is trying to filter on "employeeName", which is how you get the nullreference,
if you change your ActionResult method to return something like this then filtering works fine:
return Json(new { result = DataSource, count = TotalRecords }, new System.Text.Json.JsonSerializerOptions() { PropertyNamingPolicy = null });
Hi Jason Smith,
Thanks for updating with the solution you have tried. Please get back to us for further assistance.
Regards,
Pavithra S
Could oyu explain further where to do this at? when you return at the end, doesn't the filtering exception happen before you return the result anyways?
Hi Virang Patel,
You can resolve the casing problem serializing and returning them in the PascelCase format using the below code in the startup.cs file of the application,
If the ASP.NET CORE version is 2.X then add the below code in startup.cs file
|
public void ConfigureServices(IServiceCollection services) { services.AddMvc().AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver(); }); }
|
If the ASP.NET CORE version is 3.X and more, then add the below code in startup.cs file
|
public void ConfigureServices(IServiceCollection services) { services.AddMvc().AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver =
new DefaultContractResolver();
}
|
To use AddNewtonsoftJson in your project we need include the Microsoft.AspNetCore.Mvc.NewtonsoftJson assembly
Note : We need to install NewtonSoft.JSON as dependency since Syncfusion.EJ2.AspNet.Core dependent to NewtonSoft.JSON package.
Refer to the below documentation for more information.
Regards,
Pavithra S
Good day folks,
I had the same issue and tried for hours to fix it even with what Jason Smith comment I wasn't able to fixed.
Finally I noticed that the filter works when at list one sort is done. Otherwise the filter doesn't work.
So I added this peace of code to my javascript file :
$(document).ready(function () {
document.getElementById("myGrid").ej2_instances[0].sortSettings.properties.columns.push({ field: 'myDefaultSortingColumn', direction: 'Descending' });
})
Now, filtering is working properly when page is loaded.
Hi Ahmed,
We understand that you have resolved your issue on your own.
Please get back to us if you have further queries or if you are still facing the reported issue.
Regards
Aishwarya R