How to send anti-forgery token header for autocomplete in grid

I'm following the directions here https://ej2.syncfusion.com/aspnetcore/documentation/grid/editing/edit-types#render-autocomplete-component-while-editing to add an autocomplete component to a data grid. I've written the JS methods to connect the component, including: 

function writeTypeAC(args) {

    typeAutoCompleteEle = new ej.dropdowns.AutoComplete({

        allowCustom: true,

        filterType: "Contains",

        value: args.rowData[args.column.field],

        dataSource: new ejs.data.DataManager({

            "url": "@Model.MakeHandlerUrl("TypeSource")",

            "adaptor": new ejs.data.UrlAdaptor()}),

        fields: { value: 'ID', text: 'Name' }

    });

    typeAutoCompleteEle.dataSource.headers = [{ 'XSRF-TOKEN': $("input:hidden[name='__RequestVerificationToken']").val() }];

    typeAutoCompleteEle.appendTo(typeElement);

}



however, when the time comes to call the datasource, it doesn't send the XSRF header. Is there somewhere during the process that I can inject that header better than adding it to the dataSource as I did above?


3 Replies

AR Aishwarya Rameshbabu Syncfusion Team November 14, 2024 10:58 AM UTC

Hi R Brian Lindahl,


Greetings from Syncfusion support.


To include the anti-forgery token header for the AutoComplete component used as a cell-edit-template in the Syncfusion Grid, you can define the headers within the DataManager instance of the data bound to the AutoComplete component. Please refer to the following code lines, updated sample, and screenshot for further details.


Index.cshtml

 

    function writeTypeAC(args) {

        typeAutoCompleteEle = new ej.dropdowns.AutoComplete({

            allowCustom: true,

            filterType: "Contains",

            value: args.rowData[args.column.field],

            dataSource: new ej.data.DataManager({

            url: "/Home/UrlDataSource",

            adaptor: new ej.data.UrlAdaptor(),

            headers:[{ 'XSRF-TOKEN': "Your Token Value"}]

            }),

            fields: { value: 'CustomerID', text: 'CustomerID' }

        });

        typeAutoCompleteEle.appendTo(element);

    }

 


Sample: please find in the attachment.


Screenshot:



Documentation Link: Adding-custom-headers


If you need any other assistance or have additional questions, please feel free to contact us.


Regards

Aishwarya R


Attachment: 195169Sample_532e07a9.zip


RB R Brian Lindahl November 14, 2024 03:56 PM UTC

ok, thank you, that worked. i don't remember why i had it as a separate assignment in the first place. 


next question, about the same code: the records in the datasource have two fields, ID and Name. ID is an integer, which is the value that should be assigned to the grid column after a selection is made. Name is what should be displayed, both in the grid when not editing, and when typing in the search field to find a record. Thus, I have: 

fields: { value: 'ID', text: 'Name' }

However, there are two issues:

  1. when I get the datamanager object on the server side, the "where" query field is "ID", not "Name". I can get around this by changing the field name before I execute the where. Not elegant, but functional. If I have "value: 'Name'" in the script, the query works without modification, but it tries to send the display value to the server when updating, which is of course not right. So it seems that I do need "value: 'ID'". 
  2. If I make it by modifying the query on the server side, the value displayed in the grid when not editing is the  integer ID, not the text. 
Is there a way to do what I need? i want the non-editing display to be the text, just as it would be if I used a dropdownedit, and the value sent to the server to be the ID. I originally had this as a dropdownedit in the grid, but the number of records in the data source has grown to the point where it's almost impossible to scroll through the dropdown and find what I'm looking for in a timely manner. 


AR Aishwarya Rameshbabu Syncfusion Team November 20, 2024 01:34 PM UTC

Hi R Brian Lindahl,


Query 1: when I get the datamanager object on the server side, the "where" query field is "ID", not "Name".

Query 2: If I make it by modifying the query on the server side, the value displayed in the grid when not editing is the  integer ID, not the text. 

In your scenario, you wanted to display the text field (Name) in the Grid as well as during typing in the search field of the AutoComplete component. It is important to note that in the EJ2 AutoComplete component, the search and filter functions operate exclusively based on the value field. Mapping the text field will only update the list items' text in the popup, while search and filter actions will be conducted based on the value field. If you require filtering based on both text and value fields, you can utilize the filtering event with a custom predicate. Please refer to the provided code example, updated sample, and video demonstration where we have customized the AutoComplete component's filtering event. This setup will search for both Name and ID in the data source and return the corresponding results.


Index.cshtml

 

<script>

    var typeAutoCompleteEle;

    var element

    function create(args) {

        element = document.createElement('input');

        return element;

    }

    function read(e) {

        return typeAutoCompleteEle.text;

    }

    function destroy() {

        typeAutoCompleteEle.destroy();

    }

    function writeTypeAC(args) {

        typeAutoCompleteEle = new ej.dropdowns.AutoComplete({

            allowCustom: true,

            filterType: "Contains",

            text: args.rowData[args.column.field],

            dataSource: new ej.data.DataManager({

                url: "/Home/UrlDataSource",

                adaptor: new ej.data.UrlAdaptor(),

                headers: [{ 'XSRF-TOKEN': "Your Token Value" }]

            }),

            fields: { value: 'EmployeeID', text: 'CustomerID' },

            filtering: function (e) {

                e.preventDefaultAction = true;

                var predicate = new ej.data.Predicate('CustomerID', 'contains', e.text);

                predicate = predicate.or('EmployeeID', 'contains', e.text);

                var query = new ej.data.Query();

                //frame the query based on search string with filter type.

                query = (e.text != "") ? query.where(predicate) : query;

                //pass the filter data source, filter query to updateData method.

                e.updateData(this.dataSource, query);

            }

        });

        typeAutoCompleteEle.appendTo(element);

    }

</script>

 



Query 3: I originally had this as a dropdownedit in the grid, but the number of records in the data source has grown to the point where it's almost impossible to scroll through the dropdown and find what I'm looking for in a timely manner. 


For the dropdown edit functionality, you are encountering issues with record searches. This can be resolved by activating the allowFiltering property in the DropDownList component. Kindly refer to the code example below for this implementation.


@{

    var DropDownList = new Syncfusion.EJ2.DropDowns.DropDownList() { AllowFiltering = true };

}

<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowPaging="true" toolbar="@( new List<object>() {"Add","Edit","Delete","Update","Cancel"})">

    <e-grid-editsettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editsettings>

    <e-grid-pageSettings pageSize="15"></e-grid-pageSettings>

    <e-grid-columns>

          ……………………………

        <e-grid-column field="CustomerID"  type="string" headerText="Customer Name" type="string" width="150" editType="dropdownedit"  edit="new {@params = DropDownList }"></e-grid-column>

    </e-grid-columns>

</ejs-grid>



Document Reference:

Custom Filtering in AutoComplete Component


API References:

filtering

allowfiltering


If you need any other assistance or have additional questions, please feel free to contact us.


Regards

Aishwarya R


Attachment: 195169SampleAndVideo_29422cf2.zip

Loader.
Up arrow icon