2011-06-15 47 views
-1

我有添加到列表中的查詢。現在我需要在javascript中使用它。它正在ASP.NET 3.5中完成。所以,我需要將它作爲JSON對象發送。這是我做了什麼:(我不知道這是否是得到一個JSON對象的正確的方法糾正我,如果我錯了將LINQ查詢結果存儲爲JSON對象並傳遞給javascript

List<NCDCPoint> dupli = (from n in CDC.NCDCPoints 
          where n.EVENT_TYPE_ID == et 
          where n.BeginDate == b 
          where n.EndDate == e 
          select n).ToList<NCDCPoint>(); 
    System.Web.Script.Serialization.JavaScriptSerializer oSerializer = 
new System.Web.Script.Serialization.JavaScriptSerializer(); 
    string sJSON = oSerializer.Serialize(dupli); 





} 

這是我的jQuery的樣子

$.ajax({ 
      type: "POST", url: "Data.aspx/CheckInsertRecord", 
      data: "{EventType:'" + eventtype + "',BeginDate:'" + begindate + "'," + 
        "EndDate:'" + enddate+"' }", 
      contentType: "application/json; charset=utf-8", dataType: "json", 
      success: function (msg) { 
      alert(msg.d); 
      if(msg.d == "true") 
     { 
     Showduplicate(); 
     } 
      } 
     }); 

所以,請讓我知道接下來我需要做什麼?還有一件事是我已經返回true或false返回到aspx頁面中的$ .ajax {}那麼我應該如何發送JSON對象?

+1

-1,你的框架是什麼?這個代碼在哪裏被調用?你已經在哪裏返回true/false?你的jQuery調用是什麼樣的?服務器上的「動作」或ajax方法是什麼樣的?您需要提供更多信息。 – 2011-06-15 03:12:09

+0

更新了帖子。 – 2011-06-15 03:15:50

回答

0

更改方法返回List<NCDCPoint>和聲明的返回類型爲JSON:

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json] 
public static List<NCDCPoint> CheckInsertRecord(String EventType, String BeginDate, String EndDate) 
{ 
    NCDCPoint ncdc = new NCDCPoint(); 
    CEOSurveyDataContext CDC = new CEOSurveyDataContext(); 
    int et = Convert.ToInt32(EventType); 


    DateTime b = Convert.ToDateTime(BeginDate); 
    DateTime e = Convert.ToDateTime(EndDate); 

     bool query = (from n in CDC.NCDCPoints 
         where n.EVENT_TYPE_ID == et 
         where n.BeginDate == b 
         where n.EndDate == e 
         select n).Count()>0; 

    return = (from n in CDC.NCDCPoints 
          where n.EVENT_TYPE_ID == et 
          where n.BeginDate == b 
          where n.EndDate == e 
          select n).ToList<NCDCPoint>(); 
} 
+0

嘿,我可以同時發送「真或假」和JSON對象...否則我需要聲明一個單獨的方法 – 2011-06-15 03:23:18

+0

嘿..當我試圖使用警報(msg.d)..我什麼也沒得到......實際上它甚至沒有顯示警報。 – 2011-06-15 03:45:18

0

可以使用JavascriptSerializer serailize對象,並將其發送。

var dupli= from n in CDC.NCDCPoints 
         where n.EVENT_TYPE_ID == et 
         where n.BeginDate == b 
         where n.EndDate == e 
         select n; 

return new JavaScriptSerializer().Serialize(dupli); 

而在jquery ajax的成功回調。

... 
success:function(msg){ 
var data = $.parseJSON(msg.d); 
//And you get your object 
} 
+0

序列化「aspnet_User」類型的對象時檢測到循環引用。這是我更新代碼時得到的錯誤。 – 2011-06-15 03:37:28

相關問題