2014-05-05 61 views
0

這讓我瘋狂......作爲另一個問題的一部分,我一直致力於將ajax轉移到我的mvc4 web應用程序。WebMethod正在返回變量名稱,而不是數組

我一直用這個作爲一個例子:http://www.codeproject.com/Articles/692793/Introduction-to-Knockout-js-and-CRUD-Operations-in

模型如下:

public class ColorList 
{ 
    public int? ID; 
    public string ColorName; 
} 

控制器的功能如下:

 [WebMethod] 
    public ColorList[] GetAssignedColors(string szUserRecID) 
    { 
     int Rec = Convert.ToInt32(szUserRecID); 
     var query = (from t in db.tblColors 
        join c in db.tblUser on t.fkColorID equals c.pkColorID 
        where t.fkRecID == Rec 
        select new ViewModels.ColorList() 
        { 
         ColorName = c.szColorName, 
         ID = t.ColorID 
        }).OrderBy(c => c.ColorName); 
     //var q = query.ToArray(); // if I break and view q, the array exists 
     return query.ToArray(); 
    } 

不知道這但這裏是我的Ajax方法:

 $.ajax({ 
     type: "POST", 
     url: '@Url.Action("GetAssignedColors", "EditColors")', 
     data: { szUserRecID: RecID }, 
     success: function (results) { 
      var colors = $.map(results.d, function (item) { 
       return new Color(item) 
      }); 
      self.CurrentColors(colors); 
     }, 
     error: function (err) { 
      alert(err.status + " : " + err.statusText); 
     } 
    }) 

如果我觀看提琴手我的代碼提供該響應是(在文本視圖):

MyApps.ViewModels.ColorList []

未顏色數組作爲我期待。

爲什麼函數返回變量名稱而不是數組本身?

我已經玩過這個了,但是我的ajax調用似乎沒有解釋json響應...但是,數據顯示在這個例子中,但是在我的網頁中顯示的數據看起來不可用。

 [HttpPost] 
    public JsonResult GetAssignedColors(string szUserRecID) 
    { 
     int Rec = Convert.ToInt32(szUserRecID); 
     var query = (from t in db.tblColors 
        join c in db.tblUser on t.fkColorID equals c.pkColorID 
        where t.fkRecID == Rec 
        select new ViewModels.ColorList() 
        { 
         ColorName = c.szColorName, 
         ID = t.ColorID 
        }).OrderBy(c => c.ColorName); 
     //var q = query.ToArray(); // if I break and view q, the array exists 
     return Json(query.ToArray(), JsonRequestBehavior.AllowGet); 

    } 

由於格式化在註釋中不起作用,結果沒有必要的數據,但「數據」做到了。

 success: function (data) { 
     var colors = $.map(data, function (item) { 
      return new Color(item) 
     }); 
+0

你不應該在ASP.NET MVC中使用'WebMethodAttribute'。該屬性屬於ASP.NET Web服務技術(現在已經過時)。關於第二次嘗試,因爲您正在使用'POST',所以不需要'JsonRequestBehavior.AllowGet'。此外,參數'szUserRecID'可以是int類型而不是字符串,以便讓ASP.NET MVC完成模型綁定而不是自己執行'Convert.ToInt32'。關於AJAX調用中數據的問題,您是否嘗試過在'success'回調函數內設置斷點以查看您有哪些數據? –

+0

另外,你寫的「似乎沒有解釋JSON響應」。這是否意味着你已經檢查了'success'回調函數,並且發現'results'參數是JSON字符串而不是對象?如果是這樣,你檢查了HTTP響應的頭文件嗎?如果你閱讀關於'jQuery.ajax()'的文檔,它會說它會根據來自HTTP響應的頭文件推斷數據類型。如果您想強制它將其解釋爲特定類型,則可以將'dataType:'json''屬性添加到您的AJAX選項對象。 –

+0

我假設我應該根據您的第一條評論使用我列表中的第二個函數? 關於你的第二條評論;如何在cshtml的javascript函數中設置斷點?當我在VS2012中運行時,他們從來沒有被打過......我仍然有問題,即使我的代碼中有dataType:'json'。 Fiddler確實將數據報告爲json,並在檢查時看到json格式的數據。 – DaBlue

回答

0

我工作過的網頁有:

success: function (result) { 
    var colors = $.map(result.d, function (item) { 
     return new Color(item) 
    }); 

實際的工作方法是:

success: function (data) { 
    var colors = $.map(data, function (item) { 
     return new Color(item) 
    }); 

我沒有使用[的WebMethod]每羅伯特的智慧和所使用的json響應。

最終/工作功能是: [HttpPost] 公共JsonResult GetAssignedColors(串szUserRecID) { INT建議= Convert.ToInt32(szUserRecID); VAR查詢=(從噸db.tblColors 加入下在db.tblUser上t.fkColorID等於c.pkColorID 其中t.fkRecID ==建議 選擇新ViewModels.ColorList() { ColorName = c.szColorName , ID = t.ColorID })。OrderBy(c => c.ColorName); // var q = query.ToArray(); //如果我打破並查看q,則數組存在 return Json(query.ToArray());

} 

注意去除JsonRequestBehavior.AllowGet

感謝羅伯特!

相關問題