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

C# jqGrid:使用editurl调用RESTful方法时出现问题,c#,jqgrid-asp.net,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",

这是我用来加载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 });

 }
所有内容都正确加载,单击一行可使字段像它应该的那样可编辑。当按下enter键保存编辑时,事件似乎正确触发,并调用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):

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


我找不到其他人有这个问题,根据类似的例子,我可以发现我似乎做得很正确。在这一点上,我非常感谢所有的帮助。

我终于通过这个变通办法使它工作起来了。这并不理想,但它完成了任务。代码更改以粗体显示

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

        }
  • WCF服务只允许HTTP Post请求:因此它们不是RESTFul的,否则它们将通过GET请求中的URL传递数据
  • 您的方法表示它接受单个项,即许多字符串。这是不正确的,自动JSon绑定不够智能,无法单独绑定每个项。尝试创建具有正确属性的信息代理,并接收该代理或JSONResult对象,然后自己解析数据。如果依赖自动绑定,请确保使用最新版本的.NET frame work 3.5 SP1或更高版本,其中允许自动JSon到模型绑定

  • 资料来源:Asp.net、Microsoft MVC 3.0参考指南、msdn、trirand.com等

    当SaveTopic在操作合同中标记为“POST”时,我得到“500:内部服务器错误”。上面详细介绍了我尝试过的方法定义和相应的错误。谢谢粗体格式不起作用。仅将变更附在****中。寻找那些可以看到修改的。
    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
    
            }