简体   繁体   中英

Kendo Grid how to datasource update,create,delete

for some reasons i cannot use MVC-wrapper of Kendo grid.So i am trying to build Kendo grid on Javascript.

There are 2 main problems when trying to update or create records on grid.

1-)All operations(destroy,update,create) on grid just go to create action by Datasource's of Kendo grid.For example after update a record , datasource send data to this url to many times(number of columns): http://localhost:63186/Administrator/DefinitionDetailCreate .It should pass values to :

http://localhost:63186/Administrator/DefinitionDetailUpdate

2-)After operation(update or create) Grid sends all data to Action Method instead of new or updated data.So it sends requests the number of columns on grid

JS:

  var dataItem = this.dataItem($(e.target).closest("tr"));
        var code = dataItem.CODE;
       // alert(code);

        var crudServiceBaseUrl = "/Administrator/",
                     dataSource = new kendo.data.DataSource({
                         transport: {
                             read: {
                                 url: '@Url.Action("DefinitionDetailRead", "Administrator")',
                                 data: { DefinitionCode: code },

                                 dataType: "json"
                             },
                             update: {
                                 url: '@Url.Action("DefinitionDetailUpdate", "Administrator")' ,
                                 type: "POST",
                                 dataType: "text"
                             },
                             destroy: {
                                 url: '@Url.Action("DefinitionDetailDelete", "Administrator")',
                                 type: "POST",
                                 dataType: "text",
                             },
                             create: {
                                 url: '@Url.Action("DefinitionDetailCreate", "Administrator")',
                                 type: "POST",
                                 dataType: "text",

                             } },
                        // batch: true,
                         pageSize: 9,

                         schema: {
                             data: "Data",
                             model: {

                                 ID: "ID",
                                 fields: {
                                     ID: { editable: false, nullable: true },
                                     DESCRIPTION: { validation: { required: true } }


                                 }
                             }
                         }
                     });

        $("#detailsGrid").kendoGrid({

            dataSource: dataSource,
            attributes: { style: "padding-left: 0px; font-size: 14px"},
            pageable: {refresh: false, pageSizes: false, buttonCount: 5},
            toolbar: ["create"],
            columns: [
                {field: "DESCRIPTION", title: "DESCRIPTION", width: "200px"},
                { command: ["edit", "destroy"], title: "Operasyon", width: "100px" }],
            editable: "popup"
        });

Controller:

 [HttpPost]
    public ActionResult DefinitionDetailUpdate(Guid ID,Guid REFERENCEID,string DESCRIPTION)
    {
        return null;

    }
    [HttpPost]
     public ActionResult DefinitionDetailCreate(Guid ID, Guid REFERENCEID, string DESCRIPTION)
     {
         return null;

     }

First you might need to add parameterMap , this will help identify server side methods:

parameterMap: function (options, operation) {
var out = null;
switch (operation) {
    case "create":
        out = '{ "param":' + options.somevalue + '}';
        break;
    case "read":
        out = '{ "id":' + options.somevalue + '}';
        break;
    case "update":
        out = '{ "id":' + options.somevalue + '}';
        break;
    case "destroy":
        out = '{ "id":  ' + options.somevalue + '}';
        break;
}
return out;

}

I would also suggest to keep all the dataTypes as dataType: "json"

Also you seem to be missing contentType in your transports definitions :

  update: {
                            url: _op.serviceBaseUrl + "Update",
                            dataType: "json",
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            complete: function (jqXhr, textStatus) {

                            }
                        },

i have posted answer for the same type of question, you may have a check

or

you may Use this code

js

 var dataItem = this.dataItem($(e.target).closest("tr"));
            var code = dataItem.CODE;
           // alert(code);

            var crudServiceBaseUrl = "/Administrator/",
                         dataSource = new kendo.data.DataSource({
                             transport: {
                                 read: {

                                 url: '@Url.Action("DefinitionDetailRead", "Administrator")',
    type: "POST",
                                     dataType: "json"
                                 },
                                 update: {
                                     url: '@Url.Action("DefinitionDetailUpdate", "Administrator")' ,
                                     type: "POST",
                                     dataType: "json"
                                 },
                                 destroy: {
                                     url: '@Url.Action("DefinitionDetailDelete", "Administrator")',
                                     type: "POST",
                                     dataType: "json",
                                 },
                                 create: {
                                     url: '@Url.Action("DefinitionDetailCreate", "Administrator")',
                                     type: "POST",
                                     dataType: "json",

                                 },
                                  parameterMap: function (options, operation) {
                                  if (operation !== "read" && options.models) {
                                  return { models: kendo.stringify(options.models) };
                                  }
                              } },
                            // batch: true,
                             pageSize: 9,

                             schema: {
                                 data: "Data",
                                 model: {

                                     ID: "ID",
                                     fields: {
                                         ID: { editable: false, nullable: true },
                                         DESCRIPTION: { validation: { required: true } }


                                     }
                                 }
                             }
                         });

            $("#detailsGrid").kendoGrid({

                dataSource: dataSource,
                attributes: { style: "padding-left: 0px; font-size: 14px"},
                pageable: {refresh: false, pageSizes: false, buttonCount: 5},
                toolbar: ["create"],
                columns: [
                    {field: "DESCRIPTION", title: "DESCRIPTION", width: "200px"},
                    { command: ["edit", "destroy"], title: "Operasyon", width: "100px" }],
                editable: "popup"
            });

Controller

 [HttpPost]
    public ActionResult DefinitionDetailUpdate(string models)
    {
   //Deserialize to object
    IList<UrObjectType> objName= new JavaScriptSerializer().Deserialize<IList<UrObjectType>>(models);

     return Json(objName)


    }
    [HttpPost]
     public ActionResult DefinitionDetailCreate(string models)
     {
         //Deserialize to object
    IList<UrObjectType> objName= new JavaScriptSerializer().Deserialize<IList<UrObjectType>>(models);

     return Json(objName)

     }

Note that parameterMap: function() send updated data in serialize string format with name models so you should use " models " as parameter name in your action

i hope this will help you:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM