1

私はC#コードビハインドでlinqからエンティティへのクエリを介してSQL DBから場所のリストを取得するプログラムを持っています。このリストは、地図上の場所を表示するためにjavascriptメソッド(Google map api v3)によって解析する必要があります。私は、処理のためにサーバー側のクエリからjavascript関数に情報を取得する最善の方法を見つける必要があります。何かご意見は!?ASP.NETのJavascript処理サーバー側のデータ

編集:直列化でエラー...

JavaScriptSerializer jss = new JavaScriptSerializer(); 
      using (RamRideOpsEntities myEntities = new RamRideOpsEntities()) 
      { 
       var validDates = (from a in myEntities.AdminOptions 
            select new { a.ValidDate1, a.ValidDate2 }).First(); 

       var allWaitingRides = (from r in myEntities.Rides 
             where ((r.TimeOfCall >= validDates.ValidDate1 || 
              r.TimeOfCall <= validDates.ValidDate2) && r.Status.Equals("Waiting", StringComparison.OrdinalIgnoreCase)) 
             orderby r.TimeOfCall descending 
             select r).ToList(); 

       json_topTen.Value = jss.Serialize(allWaitingRides.GetRange(0, 10).ToArray()); 
      } 

Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection. 

Source Error: 


Line 106:          select r).ToList(); 
Line 107: 
Line 108:    json_topTen.Value = jss.Serialize(allWaitingRides.GetRange(0, 10).ToArray()); 
Line 109:   } 
Line 110:  } 

Source File: D:\DOCUMENTS\RamRide\RamRideOps_PL\RamRideOps\RamRideOps\Ops\DispatchCar.aspx.cs Line: 108 

Stack Trace: 


[ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.] 
    System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +52 
    System.Collections.Generic.List`1.GetRange(Int32 index, Int32 count) +70 
    RamRideOps.DispatchCar.setTopTen() in D:\DOCUMENTS\RamRide\RamRideOps_PL\RamRideOps\RamRideOps\Ops\DispatchCar.aspx.cs:108 
    RamRideOps.DispatchCar.Page_Load(Object sender, EventArgs e) in D:\DOCUMENTS\RamRide\RamRideOps_PL\RamRideOps\RamRideOps\Ops\DispatchCar.aspx.cs:41 
    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 
    System.Web.UI.Control.OnLoad(EventArgs e) +91 
    System.Web.UI.Control.LoadRecursive() +74 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207 

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272 

答えて

2

あなたのエンティティが十分な光であれば、単にJSON配列にそれらをシリアル化し、クライアントにそれをバックに渡します。それらがあまり明るくない場合は、データ転送オブジェクトを作成してジョブを完了します。下記の指定されたあなたのハンドラが振る舞う

$.ajax({url: "url/to/handler.ashx", 
    success: function(data, state) { 
     //data represents the array of objects sent back from the server. 
    } 
}); 

:ここ

はどのようなあなたのAJAXリクエストが(jQueryのを使用して)のように見える可能性があるのC#からデータをエクスポートする

public void ProcessRequest(HttpContext context) 
    { 
     context.Response.ContentType = "application/x-javascript"; 
     //fetch data from database. 
     //return data by writing the serialized objects directly to context.Response.OutputStream 
    } 
+0

良いチュートリアルを知りましたか? – SHeinema

+0

あなたのニーズが単純な場合は、組み込みのものを使うことができます。http://msdn.microsoft.com/en-us/library/bb412179.aspx –

+0

ok私はjsonのシリアル化をダウンしていると思いますが、それを渡すコードは何ですかクライアントに戻ってきますか? (申し訳ありませんasp.netに新しい...) – SHeinema

3

最も簡単な方法ではないかもしれませんJavaScriptでデータを消費する最も簡単な方法ですが、私は後者の観点から見てみましょう。

JavaScriptのパースペクティブからこれを行う最良の方法は、おそらくJSONPを使用してクエリの内容をJSON表現にエクスポートしてクライアントに転送することです。標準XMLHttpRequestを使用してサーバーからのデータを取得し、そこからJSで使用するためにクライアント側で解析することができます。

サイトJSON.orgには、C#で利用可能な多数のJSONパーサー/シリアライザのリストがあります。

+0

この質問をチェックしてください、興味深い:http://stackoverflow.com/questions/1212344/parse-json-in-c-sharp – buley

+0

近づいていますが、今はエラーが発生しています。 ^^それについての考えは? – SHeinema

3

asp.net mvcを使用している場合、javascript/jquery呼び出しを使用してJSONをクライアントに返すことができます。 Callng MVC from Javascript

標準のasp.netを使用している場合は、Webメソッドまたはasmxページを使用してJSONを返すことができます。データ。

あなたが標準asp.net(非MVC)を使用しているよう

が見える...上記のいずれかのエンティティへのLINQ/wの呼び出しは、その後JSONに変換し、ダウンにそれを渡してください - チェックアウトこのチュートリアル:

ASP.NET Web Methods & jquery

関連する問題