2011-09-14 24 views
0

我試圖從我的jqGrid保存行數據時,似乎遇到了設置我的web服務的問題。數據恢復正常;網格加載數據正常,當點擊鉛筆時編輯框顯示正常。我已經使用IE開發人員工具檢查了請求正文,它顯示的數據類似於:Step_Number = 1 & oper =編輯& id = 1我知道有一些東西缺少方法,我有這種方法設置和它的方式叫,但我不能爲我的生活找到我正在尋找的答案。編輯jqGrid並將數據發送到.NET MVC Webservice

我需要什麼將數據格式化爲JSON併發送它,以及我的.Net方法接受數據時必須具備哪些內容?

感謝您提供任何幫助。

[WebInvoke(Method = "POST", UriTemplate = "/Save/JSA", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
public string UpdateRecord() 
{} 

**Update** This is the one that works 
[WebInvoke(Method = "POST", UriTemplate = "/Save/JSA", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
public string UpdateJSA(string Step_Number, string Step_Description, string oper, string id) 
{} 

當我使用上述功能時,該功能可以正常工作,但沒有數據。第二個我放了一個參數,我的代碼似乎打破了。這裏是我正在使用的jqGrid代碼:

$.getJSON('FileServices/Get/JSA/' + id, function (data) { 
$("#list").jqGrid({ 
    url: 'FileServices/GetList/JSA', 
    cellEdit: true, 
    editurl: 'FileServices/Save/JSA', 
    //serializeRowData: function (data) { return JSON.stringify(data); }, 
    datatype: 'local', 
    gridComplete: function() { 
      $("#list").jqGrid('setGridParam', {}).trigger("reloadGrid"); 
    }, 
    onSelectRow: function (rowid, status) { 
    }, 
    loadComplete: function (data) { 
      var det = $("#details"); 
      $("#list").setGridWidth(det.width() - 18, true); 
    }, 
    colNames: ['Id', 'Step Number', 'Step Description'], 
    colModel: [ 
     { name: 'Id', index: 'Id', width: 30, sortable: false, hidden: true }, 
     { name: 'Step_Number', editable: true, index: 'Step_Number', align: 'center', width: 50, fixed: true, resizable: false, sortable: false, title: false, cellattr: function (rowId, tv, rawObject, cm, rdata) { return 'style="white-space: normal; vertical-align: top;' } }, 
     { name: 'Step_Description', index: 'Step_Description', sortable: false, width: 400, cellattr: function (rowId, tv, rawObject, cm, rdata) { return 'style="white-space: normal; vertical-align: top;' } } 
    ], 
    pager: '#pager', 
    rowNum: 5, 
    rowList: [5, 10, 15, 20, 25, 30, 50], 
    sortname: 'Id', 
    height: 'auto', 
    rownumbers: true, 
    autowidth: true, 
    forceFit: true, 
    shrinkToFit: true, 
    sortorder: 'asc', 
    viewrecords: true, 
    gridview: true, 
    hidegrid: false, 
    caption: '' 
}); 

$.extend($.jgrid.edit, { 
    ajaxEditOptions: { contentType: "application/json" }, 
    recreateForm: true, 
    serializeEditData: function (postData) { 
     return JSON.stringify(postData); 
    } 
}); 

var thegrid = $("#list"); 

for (var i = 0; i < data.details.length; i++) { 
    thegrid.addRowData(i + 1, data.details[i]); 
} 

thegrid.navGrid("#pager"); 
}); 

回答

1

您貼的不是所有必需的代碼。你寫了「當我點擊鉛筆時」,所以我想你在某個地方使用navGrid,但是如何以及用哪個參數?......以任何方式,你都以錯誤的方式設置jQuery.jgrid.edit。該代碼應該看起來如下:

$.extend($.jgrid.edit, { 
    ajaxEditOptions: { contentType: "application/json" }, 
    recreateForm: true, 
    serializeEditData: function (postData) { 
     return JSON.stringify(postData); 
    } 
}); 
+0

謝謝奧列格。我做了你的改變,我得到了{「Step_Number」:「1」,「oper」:「edit」,「id」:「1」}。至少現在是JSON格式。但是我仍然在調用UpdateRecord時遇到問題。如果我嘗試UpdateRecord(字符串數據),我得到一個錯誤400.我錯過了什麼是防止在UpdateRecord方法中引用數據?再次感謝Oleg。 – DrZ

+0

@DrZ:你得到你調用的'UpdateRecord'參數的名字。它應該是重要的*哪個名稱*有參數。另一個參數'BodyStyle'也很重要。嘗試刪除它或使用「WebMessageBodyStyle.WrappedResponse」。我建議你看看[答案](http://stackoverflow.com/questions/3917102/jqgrid-add-row-and-send-data-to-webservice-for-insert/3918615#3918615)和[this一個](http://stackoverflow.com/questions/5497412/consuming-json-in-wcf-service-method/5498450#5498450)(最後一個僅用於理解'BodyStyle'的含義)。 – Oleg

+0

@DrZ:我希望你也發現你不應該使用'$ .getJSON',尤其是'addRowData'來填充網格。最好的是使用'ajaxGridOptions:{contentType:'application/json; charset = utf-8'}'(參見[here](http://stackoverflow.com/questions/3161302/jqgrid-page-1-of-x-pager/3161542#3161542)和[here](http:/ /stackoverflow.com/questions/4029909/load-data-from-webservice-asmx-to-jqgrid-please-help-me/4031603#4031603)) – Oleg