Crud action

If I use this code for Antiforgery Validation and CrudUrl with remoteSaveAdaptor, action parameter, in controller, has the name of url action and not "insert", "update" or "remove".

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();
      }
 
      var adaptor = new ej.remoteSaveAdaptor().extend({
          update: function (dm, keyField, value, tableName) {
              var token = value.__RequestVerificationToken;
              delete value['__RequestVerificationToken'];
              return {
                  type: "POST",
                  url: dm.dataSource.updateUrl || dm.dataSource.crudUrl || dm.dataSource.url ,
                  data: {
                      __RequestVerificationToken: token,
                      value: value
                  }
              };
          },
          insert: function (dm, data, tableName) {
              var token = data.__RequestVerificationToken;
              delete data['__RequestVerificationToken'];
              return {
                  type: "POST",
                  url: dm.dataSource.insertUrl || dm.dataSource.crudUrl || dm.dataSource.url,
                  data: {
                      __RequestVerificationToken: token,
                      value: data
                  }
              };
          },
      })
 
      function Load(args) {
          this.model.dataSource.adaptor = new adaptor();
          this.model.dataSource.update = dmAdaptorUpdate;
          this.model.dataSource.insert = dmAdaptorInsert;
      };
 

3 Replies

VA Venkatesh Ayothi Raman Syncfusion Team January 8, 2018 06:51 AM UTC

Hi Pio, 

Thanks for using Syncfusion products. 

We went through your code example that you have shared for us and found that you are using CRUDURL to perform the CRUD operation using RemoteSaveAdaptor in Grid. Also, you are using AntiForegeryToken. 
If we are using CRUD url action method then we need to pass the corresponding action name in the Custom adaptor. But in your code example, you are missed to define the corresponding action name. This is the cause of this issue. Please refer to the following code example and Sample for your convenience which can be download from following link, 
Code example
 
var adaptor = new ej.remoteSaveAdaptor().extend({ 
        update: function (dm, keyField, value, tableName) { 
            var token = value.__RequestVerificationToken; 
            delete value['__RequestVerificationToken']; 
            return { 
                type: "POST", 
                url: dm.dataSource.updateUrl || dm.dataSource.crudUrl || dm.dataSource.url, 
                data: { 
                    __RequestVerificationToken: token, 
                    value: value, 
                    action:"update" 
                } 
            }; 
        }, 
 
         
 
        insert: function (dm, data, tableName) { 
            var token = data.__RequestVerificationToken; 
            delete data['__RequestVerificationToken']; 
            return { 
                type: "POST", 
                url: dm.dataSource.insertUrl || dm.dataSource.crudUrl || dm.dataSource.url, 
                data: { 
                    __RequestVerificationToken: token, 
                    value: data, 
                    action:"insert" 
                } 
            }; 
        }, 
         
         
    }) 

Please let us know if you have any further assistance on this. 

Regards, 
Venkatesh Ayothiraman. 



PL Pio Luca Valvona January 8, 2018 09:06 AM UTC

Thank you Venkatesh,
but when i use this code, client returns this error: "SCRIPT5007: Unable to get property 'trim' of undefined or null reference" and Dialog template freezes.

Luca


VA Venkatesh Ayothi Raman Syncfusion Team January 9, 2018 12:25 PM UTC

Hi Pio, 

Thanks for the update. 

The reported issue reproduced when we enable a primary key property to string column as well as hide the column using column’s property of Visible. We suspect that in the dialog template you have hidden the primary key input element. With string values as primary key and not updating the string primary column value during insert then the script error will occur. If so, we have logged it as an issue “Script error thrown during insert when column with string value set as primary key”. The fix for this issue will be included in the Volume 1,2018 release which is scheduled to be rolled out in the mid of February. 

Until, we suggest you to use the following workaround, set the value for the string typed primary key column as “ ” during insert by using the ActionBegin event of Grid. Please refer the code example below, 

[Index.cshtml] 
 
@(Html.EJ().Grid<object>("FlatGrid") 
        ... 
        .Columns(col => 
        { 
            col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width(90).Add(); 
            ... 
            ... 
       }) 
       .ClientSideEvents(eve => eve.ActionComplete("complete").ActionBegin("begin") ) 
        ) 
 
<script type="text/javascript"> 
    function begin(args) { 
        if (document.getElementsByClassName("e-addedrow").length && args.requestType == "save") 
        { 
            args.rowData.OrderID = ""; 
        } 
    } 
<script> 

Please refer the documentation link below, 

Please get back to us if you need further assistance. 

Regards, 
Venkatesh Ayothiraman. 


Loader.
Up arrow icon