asp.net - jqgrid 添加行并将数据发送到 webservice 进行插入

标签 asp.net jquery web-services jqgrid

我已经能够使用 jQuery/Ajax 将数据从 Web 服务中提取到 jQGrid 中。现在我想将添加/编辑的数据发送回网络服务。我已经看到一些使用 PHP 和 editurl: 命令的示例。这也适用于网络服务(就像我最初提取数据的方式一样)吗?

我已经多次查看这些示例。我什至找到了another question这与我要问的类似,但是我无法找到任何真实的例子来说明如何做我需要的事情。存在吗?

:更新:

jQuery(document).ready(function () {
    jQuery("#list").jqGrid({
        datatype: processrequest,
        mtype: 'POST',
        jsonReader: {
            root: "ListExercise", //arry containing actual data  
            page: "Page", //current page  
            total: "Total", //total pages for the query  
            records: "Records", //total number of records  
            repeatitems: false,
            id: "ID" //index of the column with the PK in it   
        },
        colNames: ['Id', 'Exercise'],
        colModel: [
      { name: 'exercise_id', index: 'exercise_id',editable:false },
      { name: 'exercise_value', index: 'exercise_value',editable:true }
      ],
        caption: 'MyFitnessApplication',
        pager: '#pager',
        rowNum: 10,
        rowList: [10, 20, 30],
        sortorder: "desc",
        viewrecords: true,
        height: '250px',
        loadonce: true,
        editurl: "../webService/exercise_ws.asmx/insertRecord"      
    }).navGrid('#pager', { edit: true, add: true, del: false });
});

如您所见,我添加了 editurl 标签。这似乎确实调用了我的网络服务。现在我不知道如何将实际参数传递给网络服务。我遗漏了一些东西,非常感谢您的帮助!

最佳答案

首先,您可以更改用于添加/编辑的一些默认选项:

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);
    }
});

(其中 JSON.stringifyhttp://www.json.org/js.html 中定义的函数)。然后将发送到服务器的数据进行 JSON 编码。几乎相同的设置可用于删除

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);
    }
});

现在您可以像下面一样定义insertRecord

[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
    }
}

您没有写出哪种类型具有exercise_id:整数或字符串。如果您使用字符串 ids,上面的代码应该稍微改变一下。

关于asp.net - jqgrid 添加行并将数据发送到 webservice 进行插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3917102/

相关文章:

c# - ASP.NET 客户端验证与服务器端验证

c# - 将 ICollection 成员与其自身进行比较

c# - ASP.Net Web 应用程序的插件

javascript - JQuery:打开/关闭菜单,打开但不关闭

jquery - 为什么 jQuery animate 不能与 cssHooks 一起使用?

java - 如何从 xml 请求中删除 xmlns =“”

c# - 为什么我能够从以 64 位模式在 IIS7 中运行的 AnyCPU .NET Web 应用程序调用 32 位 COM 库?

javascript - .keyup jquery 不适用于 Iphone

java - 使用 Jersey 从客户端调用 RESTFul Web 服务的问题

java Holder vs document-style,或者为什么要使用Holders