0

这是我用来加载我的 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:方法不允许状态:错误”

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

4

2 回答 2

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

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

于 2011-06-23T17:38:27.697 回答
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

        }
于 2011-01-05T16:38:46.873 回答