jqGrid:使用editurl调用RESTful方法时出现问题

时间:2011-01-02 16:56:49

标签: c# jqgrid-asp.net

这是我用来加载jqGrid的代码:

 function getTopics() {
     var fid = document.getElementById("SelectFunction").value;
     //alert(fid);
     $.ajax({
         url: "Restful.svc/GetTopics",
         data: { functionID: fid },
         dataType: "json",
         type: "GET",
         contentType: "application/json; charset=utf-8",
         success: function (data, status, xHR) {

             var thegrid = jQuery("#editgrid")[0];

             thegrid.addJSONData(JSON.parse(data.d));
             $("#editgrid").fluidGrid({ example: "#outerContainer", offset: -10 });
         },
         error: function (xHR, status, err) {
             alert("ERROR: " + status + ", " + err);
         }
     });
 }

 function LoadDataIntoGrid() {

     var lastcell;

     jQuery("#editgrid").jqGrid('GridUnload');

         jQuery("#editgrid").jqGrid({
             datatype: getTopics,
             height: '300px',
             colNames: ['TopicID', 'Topic', 'Description', 'Topic Entity', 'Inactive'],
             colModel: [
                    { name: 'TopicID', index: 'TopicID', width: 200, editable: false, editoptions: { readonly: true, size: 10} },
                    { name: 'TopicCode', index: 'TopicCode', width: 100, editable: true, editoptions: { size: 10} },
                    { name: 'TopicDescription', index: 'TopicDescription', width: 200, editable: true, editoptions: { size: 30} },
                    { name: "TopicEntity", index: "TopicEntity", width: 200, editable: true, resizable: true, edittype: "select", editoptions: { value: returnEntityList()} },
                    { name: 'Inactive', index: 'Inactive', width: 60, align: "center", editable: true, edittype: "checkbox", formatter: 'checkbox', formatoptions: { disabled: true} }
                    ],
             rowNum: 30,
             rowList: [10, 20, 30],
             pager: $('#pagernav'),
             sortname: 'Topic',
             viewrecords: true,
             sortorder: "desc",
             caption: "Topics",
             editurl: "Restful.svc/SaveTopic",
             onSelectRow: function (id) {
                 if (id && id !== lastcell) {
                     jQuery('#editgrid').jqGrid('restoreRow', lastcell);
                     jQuery('#editgrid').jqGrid('editRow', id, true);
                     lastcell = id;
                 } 
             }
         }).navGrid('#pagernav', { edit: false, add: true, del: false });

 }

所有内容都正确加载,点击一行会使字段可编辑。当按下回车键以保存编辑时,事件似乎正确触发,并调用editurl属性中引用的“SaveTopic”方法。此时我收到了错误。

如果SaveTopic定义如下:

    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
    public void SaveTopic( string TopicCode, string TopicDescription, string TopicEntity, string Inactive, string oper, string id)
    {
        //Code Here
    }

我从jqGrid收到此错误:“错误行:3结果:500:内部服务器错误状态:错误”

如果SaveTopic定义如下(方法更改为GET):

    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
    public void SaveTopic( string TopicCode, string TopicDescription, string TopicEntity, string Inactive, string oper, string id)
    {
        //Code Here
    }

我从jqGrid得到此错误:“错误行:3结果:405:方法不允许状态:错误”

我找不到其他人有这个问题,根据类似的例子,我发现我似乎正确地做到了。此时非常感谢所有帮助。

2 个答案:

答案 0 :(得分:1)

  1. WCF服务仅允许HTTP Post请求:因此它们不是RESTFul,否则它们将通过GET请求中的URL传递数据。
  2. 你的方法说它接受单个项目,即很多字符串。这是不正确的,自动JSon绑定不够智能,无法单独绑定每个项目。尝试使用正确的属性创建信息代理,并接收该对象或JSONResult对象并自行解析数据。如果您依赖自动绑定,请确保使用最新版本的.NET框架工作3.5 SP1或更高版本,其中允许自动JSon到模型绑定。
  3. 来源:Asp.net,Microsoft MVC 3.0参考指南,msdn,trirand.com等。

答案 1 :(得分:0)

我终于设法使用这项工作使其工作。它并不理想,但它完成了工作。代码更改为粗体。

function getTopics() 
     var fid = document.getElementById("SelectFunction").value;
     //alert(fid);
     $.ajax({
         url: "Restful.svc/GetTopics",
         data: { functionID: fid },
         dataType: "json",
         type: "GET",
         contentType: "application/json; charset=utf-8",
         success: function (data, status, xHR) {

             var thegrid = jQuery("#editgrid")[0];

             thegrid.addJSONData(JSON.parse(data.d));
             $("#editgrid").fluidGrid({ example: "#outerContainer", offset: -10 });
         },
         error: function (xHR, status, err) {
             alert("ERROR: " + status + ", " + err);
         }
     });
 }

 function LoadDataIntoGrid() {

     var lastcell;

     jQuery("#editgrid").jqGrid('GridUnload');

         jQuery("#editgrid").jqGrid({
             datatype: getTopics,
**ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },**
             height: '300px',
             colNames: ['TopicID', 'Topic', 'Description', 'Topic Entity', 'Inactive'],
             colModel: [
                    { name: 'TopicID', index: 'TopicID', width: 200, editable: false, editoptions: { readonly: true, size: 10} },
                    { name: 'TopicCode', index: 'TopicCode', width: 100, editable: true, editoptions: { size: 10} },
                    { name: 'TopicDescription', index: 'TopicDescription', width: 200, editable: true, editoptions: { size: 30} },
                    { name: "TopicEntity", index: "TopicEntity", width: 200, editable: true, resizable: true, edittype: "select", editoptions: { value: returnEntityList()} },
                    { name: 'Inactive', index: 'Inactive', width: 60, align: "center", editable: true, edittype: "checkbox", formatter: 'checkbox', formatoptions: { disabled: true} }
                    ],
             rowNum: 30,
             rowList: [10, 20, 30],
             pager: $('#pagernav'),
             sortname: 'Topic',
             viewrecords: true,
             sortorder: "desc",
             caption: "Topics",
             editurl: "Restful.svc/SaveTopic",
             onSelectRow: function (id) {
                 if (id && id !== lastcell) {
                     jQuery('#editgrid').jqGrid('restoreRow', lastcell);
                     jQuery('#editgrid').jqGrid('editRow', id, true);
                     lastcell = id;
                 } 
             }
         }).navGrid('#pagernav', { edit: false, add: true, del: false });

 }
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
        public void SaveTopic() {

            **int FunctionID = Convert.ToInt32(HttpContext.Current.Request.Form["FunctionID"]);
            int TopicID = Convert.ToInt32(HttpContext.Current.Request.Form["TopicID"]);
            string TopicCode = HttpContext.Current.Request.Form["TopicCode"];
            string TopicDescription = HttpContext.Current.Request.Form["TopicDescription"];
            int TopicEntity = Convert.ToInt32(HttpContext.Current.Request.Form["TopicEntity"]);
            string Inactive = HttpContext.Current.Request.Form["Inactive"];
            string oper = HttpContext.Current.Request.Form["oper"];
            string id = HttpContext.Current.Request.Form["id"];**

            //Code here

        }