2012-12-07 30 views
1

我已經搜索了很多論壇和帖子,我仍然無法獲得edittype:select使用editoptions:dataurl工作。jqgrid編輯類型:選擇不從dataurl保存價值

當網格填充時,數據加載就會很好。當我點擊編輯按鈕時,下拉列表中有正確的值和名稱。當我點擊保存時,我收到一個錯誤,指出其中一個參數丟失。它似乎沒有拉動所選選項的值。

我已經嘗試過格式化程序:'選擇'選項,但是當我使用沒有什麼東西顯示在網格加載時的那些列中。

這是我的代碼。

var buildSelectFromJson = function (data) { 
    var html = '<select>', d = data.d, length = d.length, i = 0, item; 
    for (; i < length; i++) { 
     item = d[i]; 
     html += '<option value=' + item.id + '>' + item.value + '</option>'; 
    } 
    html += '</select>'; 
    return html; 
}; 

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

jQuery("#usage").jqGrid({ 
    url: '/vochaptracker/Services/vochapService.asmx/GetUsage', 
    datatype: 'json', 
    mtype: 'post', 
    loadonce: true, 
    ajaxGridOptions: { contentType: 'application/json; charset=utf-8' }, 
    serializeGridData: function (postData) { 
     return JSON.stringify(postData); 
    }, 
    jsonReader: { 
     root: function (obj) { return obj.d.rows; }, 
     page: function (obj) { return obj.d.page; }, 
     total: function (obj) { return obj.d.total; }, 
     records: function (obj) { return obj.d.records; } 
    }, 
    colNames: ['Date', 'Paint', 'Booth', 'Gallons Used'], 
    colModel: [ 
     { name: 'date', index: 'date', width: 75, editable: true }, 
     { name: 'paint', index: 'paint', width: 300, editable: true, edittype: 'select', formatter:'select', editoptions: { dataUrl: "/vochaptracker/Services/vochapService.asmx/GetPaintsForEdit", 
      buildSelect: buildSelectFromJson} 
     }, 
     { name: 'booth', index: 'booth', width: 100, editable: true, edittype: 'select', formatter:'select', editoptions: { dataUrl: "/vochaptracker/Services/vochapService.asmx/GetBoothsForEdit", 
      buildSelect: buildSelectFromJson}}, 
     { name: 'gallonsUsed', index: 'gallonsUsed', width: 100, editable: true } 
    ], 
    rowNum: 20, 
    rowList: [20, 30, 40], 
    pager: '#pager4', 
    sortname: 'ID', 
    viewrecords: true, 
    sortorder: "desc", 
    caption: "Daily Usage", 
    height: '400', 
    editurl: '/vochaptracker/Services/vochapService.asmx/EditUsage', 
    ajaxSelectOptions: { contentType: "application/json", dataType: 'json', type: "POST" } 
}); 
jQuery("#usage").jqGrid('navGrid', "#pager4", { edit: false, add: false, del: true }); 
jQuery("#usage").jqGrid('inlineNav', "#pager4"); 

Web服務:

public class jqGridPaintHelper 
{ 
    public string total; 
    public string page; 
    public string records; 
    public List<TableRow> rows; 
} 

public class TableRow 
{ 
    public string id; 
    public List<string> cell; 
} 

public class editHelper 
{ 
    public string id; 
    public string value; 
} 

    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public jqGridPaintHelper GetUsage(int page, int rows, string sidx, string sord) 
    { 
     vochapdbDataContext db = new vochapdbDataContext(); 
     jqGridPaintHelper helper = new jqGridPaintHelper(); 
     int dbCount = db.DailyUsages.Count(); 
     helper.total = ((dbCount + rows - 1)/rows).ToString(); 
     helper.page = page.ToString(); 
     helper.records = dbCount.ToString(); 

     List<TableRow> usage = new List<TableRow>(dbCount); 
     foreach (DailyUsage row in db.DailyUsages) 
     { 
      usage.Add(new TableRow() 
      { 
       id = row.ID.ToString(), 
       cell = new List<string> { 
        row.date.ToShortDateString(), 
        row.Paint.paintName.ToString(), 
        row.Booth.tag.ToString(), 
        row.gallonsUsed.ToString() 
       } 
      }); 
     } 

     helper.rows = usage; 

     return helper; 
    } 

    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public int EditUsage(string ID, string date, string paintID, string boothID, string gallonsUsed, string oper, string id) 
    { 
     vochapdbDataContext db = new vochapdbDataContext(); 

     if (oper == "edit") 
     { 
      db.updateUsage(int.Parse(ID), DateTime.Parse(date), paintID, int.Parse(boothID), decimal.Parse(gallonsUsed)); 
     } 
     else if (oper == "add") 
     { 
      DailyUsage newUsage = new DailyUsage(); 
      newUsage.date = DateTime.Parse(date); 
      newUsage.paintID = paintID; 
      newUsage.boothID = int.Parse(paintID); 
      newUsage.gallonsUsed = decimal.Parse(gallonsUsed); 

      db.DailyUsages.InsertOnSubmit(newUsage); 
      db.SubmitChanges(); 
     } 
     else if (oper == "del") 
     { 
      db.deleteUsage(int.Parse(id)); 
     } 

     return 1; 
    } 

    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public List<editHelper> GetPaintsForEdit() 
    { 
     vochapdbDataContext db = new vochapdbDataContext(); 

     List<editHelper> paints = new List<editHelper>(); 

     foreach (Paint row in db.Paints) 
     { 
      paints.Add(new editHelper() { id = row.ID, value = row.paintName }); 
     } 

     return paints; 
    } 

    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public List<editHelper> GetBoothsForEdit() 
    { 
     vochapdbDataContext db = new vochapdbDataContext(); 

     List<editHelper> booths = new List<editHelper>(); 

     foreach (Booth row in db.Booths) 
     { 
      booths.Add(new editHelper() { id = row.ID.ToString(), value = row.tag }); 
     } 

     return booths; 
    } 

這是我的錯誤,當我嘗試保存一行編輯後:

System.InvalidOperationException: Missing parameter: paintID. 
at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) 
at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request) 
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() 
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() 

任何幫助是極大的讚賞。我一直堅持了大約一個月。

回答

1

我終於明白了。我的下拉列表中有一些奇怪的值。

我補充說:「周圍的價值觀,它解決了這一問題

老:

html += '<option value=' + item.id + '>' + item.value + '</option>'; 

新:

html += '<option value="' + item.id + '">' + item.value + '</option>';