ASP.NET 中的 Javascript 处理服务器端数据

发布于 2024-12-28 10:14:15 字数 3116 浏览 5 评论 0原文

我有一个程序,它通过 ac# 代码隐藏中的 linq-to-entities 查询从 sql 数据库获取位置列表。该列表需要通过 JavaScript 方法(Google 地图 api v3)进行解析,以便在地图上显示位置。我需要找到从服务器端查询获取信息到 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

I have a program which gets a list of locations from an sql db via a linq-to-entities query in a c# code-behind. This list needs to be parsed by a javascript method (Google maps api v3) in order to display the locations on a map. I need to find the best way to get the info from the server-side query to the javascript function for processing. Any thoughts!?

Edit: error on serialization...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

送你一个梦 2025-01-04 10:14:15

从 C# 导出数据的最简单方法可能不是在 JavaScript 中使用数据的最简单方法,但我将从后一个角度尝试一下。

从 JavaScript 角度来看,最好的方法可能是将查询内容导出为 JSON 表示形式,可能使用 JSONP 将其传输到客户端。您可以使用标准 XMLHttpRequest 从服务器获取该数据,然后在客户端解析该数据以在 JS 中使用。

网站 JSON.org 提供了许多 C# 中可用的 JSON 解析器/序列化器的列表。

The easiest way to export data from C# might not be the easiest way to consume data in JavaScript, but I'll give it a stab from the latter point of view.

Likely the best way to do this from the JavaScript perspective would be to export the contents of your query to a JSON representation, perhaps using JSONP to transfer it to the client. You can use a standard XMLHttpRequest to GET that data from your server, and from there parse it on the client side for use in JS.

The site JSON.org has a list of many JSON parsers/serializers available in C#.

小苏打饼 2025-01-04 10:14:15

如果您使用的是 asp.net mvc,那么您可以使用 javascript/jquery 调用将 JSON 返回给客户端。以下是 MVC 的一位大专家 (Phil Haack) 提供的有关如何完成此操作的提示:
从 Javascript 调用 MVC< /a>

如果您使用标准 asp.net,您可以使用 Web 方法或 asmx 页面返回 JSON 数据。

使用 linq 对上述任一实体进行调用,然后转换为 json 并将其向下传递...

看起来您正在使用标准 asp.net(非 mvc) - 查看本教程:

ASP.NET Web 方法和方法jquery

If you're using asp.net mvc then you can return JSON back to the client using a javascript/jquery call. Here is a tip on how this can be done from one of the big experts on MVC (Phil Haack):
Callng MVC from Javascript

If you're using standard asp.net you could use web methods or asmx pages to return your JSON data.

Make the call w/ linq to entities in either of the above then convert to json and pass it on down...

Looks like you're using standard asp.net (non mvc) - check out this tutorial:

ASP.NET Web Methods & jquery

爺獨霸怡葒院 2025-01-04 10:14:15

如果您的实体足够轻,只需将它们序列化为 JSON 数组并将其传递回客户端即可。如果它们不是很轻,请创建一些数据传输对象来完成工作。

您的 ajax 请求可能如下所示(使用 jQuery):

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

您的处理程序的行为如下:

    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
    }

If your entities are light enough, just serialize them into a JSON array and pass that back to the client. If they aren't very light, create some data transfer objects to get the job done.

Here is what your ajax request could look like (using jQuery):

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

Your handler would behave as specified below:

    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
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文