Return values from Grid Create call

3 posts, 1 answers
  1. Keith
    Keith avatar
    33 posts
    Member since:
    Jan 2013

    Posted 20 May 2013 Link to this post

    Hi,

    I'm using a Kendo Grid with MVVM bindings.  In my schema I have an ID field defined.  When I call the create server call a new record is created.  A structure is then returned from WS with one property which is the new records ID.  My code is:

    var myDS = new kendo.data.DataSource({
                    schema: {
                        model: {
                            id: "GROUP_ID",
                            fields: {
                                "GROUP_ID": {
                                    type: "number"
                                },
                                "DESCRIPTION": {
                                    type: "string"
                                },
                            }
                        }
                    },
                    pageSize: 20,
                    batch: false,
                    transport: {
                    read:{
                            url: "/Role/GetRoles"
                    },
                    update: {
                        url: "/Role/Update"
                        contentType: "application/json",
                        type: "POST"
                    },
                    destroy: {
                        url: "/Role/Delete"
                        contentType: "application/json",
                        type: "POST",
                    },
                    create: {
                        url: "/Role/Create"
                        contentType: "application/json",
                        type: "POST",
                    },
                    parameterMap: function (data, operation) {
                        if (operation != "read") {
                            return JSON.stringify(data);
                        } else {
                            return JSON.stringify(data); //return stringified options to the server
                        }
                    }
                }
                });

    var vm = kendo.observable({
                    groupData: myDS
                });


    $("#grid").kendoGrid({
                    columns: [
                        { "field": "DESCRIPTION", "title": "Group Name" },
                        { "command": "edit" },
                        { "command": "destroy" },
                    ],
                    pageable: true,
                    navigatable: true,
                    toolbar: ["create"],
                    editable: { "mode": "popup", "template": $("#popup_editor").html()},               
                });

                kendo.bind($('#grid'), vm);
                

    I am having several problems

    1) If my web service (in this case a C# MVC controller) returns anything at all I get javascript errors, the create dialog does not disappear.   If I change it to have a void return type everything works.
    2) If I edit a record after creating the Update call to my web service is never made.

    I think both issues are related in that I want to be able to sync the new ID of the newly created record back with the grid. 
  2. Keith
    Keith avatar
    33 posts
    Member since:
    Jan 2013

    Posted 21 May 2013 Link to this post

    I've gotten this working to a point.  If for my read operation I return an array of objects, then for the edit and create I return an instance of the edited/created object.  It appears it has to be the entire object, then my crud operations seem to work.

    My issue is that I want to return more than just an object representing the rows data, I would also like to return any error messages that may have occurred.  I know I can specify on the data source the error field like:

    errors: function (response) {
             return response.Message;
    }

    and I can specify the Data field in a function like:

    data: function (data) { 
             return data.List || [];
    }

    However I can't get this to work as the grid get's out of sync, I need to chang dat

    To Summarize, for the read I return data similar to:

    [{"GROUP_ID":1,"DESCRIPTION":"GROUP1"},
    {"GROUP_ID":2,"DESCRIPTION":"GROUP2"}}

    Then for create/update I return the following structure:
    {"GROUP_ID":1,"DESCRIPTION":"GROUP1"}

    I want to be able to change this to be able to return additional information on create/edit (status messgae etc). e.g. I'd have something like:
    {Message: "Success", Data: {"GROUP_ID":1,"DESCRIPTION":"GROUP1"}}
  3. Answer
    Keith
    Keith avatar
    33 posts
    Member since:
    Jan 2013

    Posted 21 May 2013 Link to this post

    Finally got to the bottom of this issue.

    Since I wanted to add different data (e.g. a return status) to the create and update calls. I need to change the schema.data to:

    data: function (data) { //specify the array that contains the data
                 return data.data || data;
    }

    This is to cater for when it's just the data (read) or when the data is a child of another structure (create/update)
Back to Top