<div style="margin-top:30px"> @(Html.EJ().Grid<object>("Grid") .Datasource(d=>d.URL("/Home/DataSource").Adaptor("UrlAdaptor")) . . . . .ClientSideEvents(eve=>eve.Load("load")) ) </div>
<script type="text/javascript">
//custom adaptor implementation extending url adaptor
var customAdaptor = new ej.UrlAdaptor().extend({
processQuery: function (dm, query, hierarchyFilters) { var obj = ej.UrlAdaptor.prototype.processQuery(dm, query, hierarchyFilters); // reused process query from url adaptor var data = ej.parseJSON(obj.data); data.__RequestVerificationToken = $('input[name=__RequestVerificationToken]').val();//appended additional parameter to the request payload
return { data: JSON.stringify(data), url: obj.url, ejPvtData: obj.ejPvtData, type: "POST", contentType: "application/json; charset=utf-8" } } });
function load(args) { this.model.dataSource.adaptor = new customAdaptor(); } [ValidateAntiForgeryToken] public ActionResult DataSource(DataManager dm) |
<script type="text/javascript"> var dmAdaptorUpdate = function (keyField, value, tableName) { var res = this.adaptor.update(this, keyField, value, tableName);
return $.ajax($.extend({ beforeSend: ej.proxy(this._beforeSend, this) }, res)); } var dmAdaptorInsert = function (data, tableName) { var res = this.adaptor.insert(this, data, tableName); var deffer = $.Deferred(); $.ajax($.extend({ beforeSend: ej.proxy(this._beforeSend, this), success: ej.proxy(function (record, status, xhr, request) { record = function () { if (data.d) data = data.d; return data; }; deffer.resolveWith(this, [{ record: record, dataManager: this }]); }, this), error: function (e) { deffer.rejectWith(this, [{ error: e, dataManager: this }]); } }, res));
return deffer.promise(); } //custom adaptor implementation extending url adaptor
var customAdaptor = new ej.UrlAdaptor().extend({
update: function (dm, keyField, value, tableName) { return { type: "POST", url: dm.dataSource.updateUrl || dm.dataSource.crudUrl || dm.dataSource.url, data: { __RequestVerificationToken: value.__RequestVerificationToken, //appending the AntiForgery token to the request payload during update action value: JSON.stringify(value) } }; }, insert: function (dm, data, tableName) { return { type:"POST", url: dm.dataSource.insertUrl || dm.dataSource.crudUrl || dm.dataSource.url, data: { __RequestVerificationToken: data.__RequestVerificationToken, //appending the AntiForgery token to the request payload during insert action value: JSON.stringify(data) } }; } });
function load(args) { this.model.dataSource.adaptor = new customAdaptor(); this.model.dataSource.update = dmAdaptorUpdate; this.model.dataSource.insert = dmAdaptorInsert; } |