jqgrid add row and send data to webservice for insert

First of all you can change some default options used for add/edit:

jQuery.extend(jQuery.jgrid.edit, {
    ajaxEditOptions: { contentType: "application/json" },
    recreateForm: true,
    serializeEditData: function (postData) {
        if (postData.exercise_value === undefined) { postData.exercise_value = null; }
        return JSON.stringify(postData);
    }
});

(where JSON.stringify is the functions defined in http://www.json.org/js.html). Then the data, which will be send to the server, will be JSON encoded. Almost the same settings can be used for the delete

jQuery.extend(jQuery.jgrid.del, {
    ajaxDelOptions: { contentType: "application/json" },
    serializeDelData: function (postData) {
        if (postData.exercise_value === undefined) { postData.exercise_value = null; }
        return JSON.stringify(postData);
    }
});

Now you can define insertRecord like following

[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public int ModifyData (string exercise_value, string oper, string id)
{
    if (String.Compare (id, "_empty", StringComparison.Ordinal) == 0 ||
        String.Compare (oper, "add", StringComparison.Ordinal) == 0) {

        // TODO: add new item with the exercise_value and return new id
        //       as the method result
    }
    else if (String.Compare (oper, "edit", StringComparison.Ordinal) == 0) {
        // TODO: modify the data identified by the id
    }
    else if (String.Compare (oper, "del", StringComparison.Ordinal) == 0) {
        // TODO: delete the data identified by the id
    }
}

You don’t wrote which type has exercise_id: integer or string. If you use string ids the code above should be changed a little.

Leave a Comment