Articles in this section
Category / Section

How to send custom headers to server using DataManager in LightSwitch HTML ?

2 mins read

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

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied