How to send custom headers to server using DataManager in .NET MVC?
You can pass any values in the Headers of ejDataManager and retrieve them at the server-side using the Request object of the HttpRequestMessage. This can be added to the headers either in the initial load of ejGrid or while performing any server-side operations like editing/adding and retrieve them at the server-side as follows.
<div id="Grid"></div>
$("#Grid").ejGrid({ dataSource: ej.DataManager({ url: "/Home/DataSource", adaptor: new ej.UrlAdaptor() }), allowScrolling: true, allowFiltering: true, allowPaging: true, load: "load", columns: [ { field: "OrderID" }, { field: "ShipCountry" }, { field: "CustomerID" }, { field: "EmployeeID" }, { field: "ShipCity" } ] });
At the load event of Grid, we have to add some values to the headers of DataManager instances.
MVC
@(Html.EJ().Grid<OrdersView>("Grid") .Datasource(ds=>ds.URL("/Home/DataSource").Adaptor(AdaptorType.UrlAdaptor)) .AllowPaging() .Columns(col => { col.Field("OrderID").Add(); col.Field("ShipCountry").Add(); col.Field("CustomerID").Add(); col.Field("EmployeeID").Add(); col.Field("ShipCity").Add(); }) .ClientSideEvents(events => events.Load("load")) )
Controller
public ActionResult DataSource(Syncfusion.JavaScript.DataManager dm) { IEnumerable Data = new NorthwindDataContext().OrdersViews.ToList(); var Field = Request.Headers.GetValues("field").ToList()[0]; var Valid = Request.Headers.GetValues("IsValid").ToList()[0]; DataResult result = new DataResult(); DataOperations operation = new DataOperations(); result.count = Data.AsQueryable().Count(); if (dm.Skip != null) Data = operation.PerformSkip(Data, dm.Skip); if(dm.Take != null) Data = operation.PerformTake(Data, dm.Take); result.result = Data; return Json(result, JsonRequestBehavior.AllowGet); }
ASPX
<ej:Grid id="Grid" runat="server" AllowPaging="true"> <DataManager URL="Default.aspx/Data" Adaptor="WebMethodAdaptor" /> <Columns> <ej:Column Field="OrderID" /> <ej:Column Field="ShipCountry" /> <ej:Column Field="CustomerID" /> <ej:Column Field="EmployeeID" /> <ej:Column Field="ShipCity" /> </Columns> <ClientSideEvents Load="load" /> </ej:Grid>
CodeBehind
In static methods, we cannot get the HttpRequest instance, so we have used the HttpContext.
[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static object Data(Syncfusion.JavaScript.DataManager value) { var Field = HttpContext.Current.Request.Headers.GetValues("field").ToList()[0]; var Valid = HttpContext.Current.Request.Headers.GetValues("IsValid").ToList()[0]; IEnumerable Data = OrderRepository.GetAllRecords(); int count = Data.AsQueryable().Count(); DataOperations operation = new DataOperations(); Data = operation.Execute(Data, value); return new { result = Data, count = count }; }
<script> function load(args) { //At the initial load of Grid, header is undefined this.model.dataSource.dataSource.headers = [];//So define them as array this.model.dataSource.dataSource.headers.push({ "field": "OrderID"});//pushing Some JSON Object this.model.dataSource.dataSource.headers.push({ "IsValid": true });//pushing Some JSON Object } </script>
Below screenshot shows the header values in the post request.
Figure 1: Header values in Post Request
Figure 2: Grid
Conclusion
I hope you enjoyed learning about how to send custom headers to server using DataManager in .NET MVC.
You can refer to our ASP.NET MVC 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 example to understand how to create and manipulate
data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!