2015-07-09 26 views
0

我從控制器返回一個字符串jqGrid,但不能填充下拉列表。在查看從MVC控制器填充編輯表單中的jqGrid選擇列表

電網代碼:

 { 
      name: 'FundCode', index: 'FundCode', editable: true, edittype: "select", 
      editoptions: { 
       dataUrl: '@Url.Action("PopulateDdl", "FundCode")' 
      }, 
      buildSelect: function (response) { 
       var data = typeof response === "string" ? $.parseJSON(response.responseText) : response, s = "<select>"; 

       s += '<option value="0">--Select Fund--</option>'; 

       $.each(data, function() { 
        s += '<option value="' + data + '">' + data + '</option>'; 

       }) 
      } 
     } 

控制器方法返回的字符串:

[HttpGet] 
    public JsonResult PopulateDdl() 
    { 
     var fundCodes = repo.GetFundCodes(); 

     var codeList = new List<string>(); 

     foreach (var t in fundCodes) 
     { 
      codeList.Add(t.FundCode.ToString()); 
     } 

     return Json(codeList, JsonRequestBehavior.AllowGet); 
    } 

這將返回一個列表格,但是我得到一個錯誤,「語法錯誤,無法識別的表達式: 「1」,「2」,「3」等]

我也嘗試返回作爲MVC SelectListItem到相同的結果

更新 - 我添加了結束選擇。它沒有解決這個問題。然後我注意到'buildSelect'與dataUrl不在同一個組中。我移動它,錯誤改變了。現在的錯誤是「意外令牌U」它說,這是上線1

新視圖代碼:

 { 
      name: 'FundCode', index: 'FundCode', editable: true, edittype: "select", 
      editoptions: { 
       dataUrl: '@Url.Action("PopulateDdl", "FundCode")', 
       buildSelect: function (response) { 
        var data = typeof response === "string" ? $.parseJSON(response.responseText) : response, s = "<select>"; 
        s += '<option value="0">--Select Fund--</option>'; 
        $.each(data, function() { 
         s += '<option value="' + data + '">' + data + '</option>'; 
        }) 
        s += "</select>"; 
       } 
      } 
     }, 

回答

3

你的問題可能與s = "<select>",您正在打開的標籤,但從來沒有關閉它,這可以毀掉你的HTML,嘗試,如果關閉該選擇可以解決這一問題:

{ 
      name: 'FundCode', index: 'FundCode', editable: true, edittype: "select", 
      editoptions: { 
       dataUrl: '@Url.Action("PopulateDdl", "FundCode")' 
      }, 
      buildSelect: function (response) { 
       var data = typeof response === "string" ? $.parseJSON(response.responseText) : response, s = "<select>"; 

       s += '<option value="0">--Select Fund--</option>'; 

       $.each(data, function() { 
        s += '<option value="' + data + '">' + data + '</option>'; 

       }) 

       s += '</select>'; // <<<<<<< close the select 
      } 
     } 

編輯

當JSON.parse的值實際上未定義時,通常會看到該錯誤。所以,我會檢查正試圖解析這個代碼 - 最有可能你沒有正確地分析實際的字符串:

  • 確保有在response.responseText有效數據
  • 確保數據實際上並沒有在response變量中,如果是這樣的話,那麼你應該不是 首先使用response.responseText
  • 確保數據還沒有以json格式存在,並且你正在重新解析它,這很可能是因爲你在JAVA中使用Json函數後返回數組。
  • 建設的選擇後,你將爲了使用return s它生效
+0

你知道一個教程,說明如何做服務器端的東西,即所以它得到的準備清單客戶端以及定義的json? – BattlFrog

+0

目前得到客戶端的字符串看起來像這樣:[「8:達拉斯;」,「10:丹佛;」,「34:密歇根州;」,....] – BattlFrog

+0

@BattlFrog我想一些例子[tutorial1] (http://www.mkyong.com/tutorials/java-json-tutorials/),[tutorial2](http://www.tutorialspoint.com/json/json_java_example.htm),[tutorial3](http:// iviewsource.com/codingtutorials/getting-started-with-javascript-object-notation-json-for-absolute-beginners/)會幫助你,如果有什麼不清楚的地方,隨時問我會很高興幫助 :) – KAD

相關問題