【问题标题】:Javascript processing server-side data in ASP.NET在 ASP.NET 中处理服务器端数据的 Javascript
【发布时间】:2012-02-15 11:15:42
【问题描述】:

我有一个程序,它通过 c# 代码隐藏中的 linq-to-entities 查询从 sql db 获取位置列表。此列表需要通过 javascript 方法(Google maps 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

【问题讨论】:

    标签: c# javascript asp.net google-maps-api-3 serverside-javascript


    【解决方案1】:

    如果您的实体足够轻巧,只需将它们序列化为 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
        }
    

    【讨论】:

    • 如果您的需求很简单,您可以使用内置的 - msdn.microsoft.com/en-us/library/bb412179.aspx
    • 好的,我想我已经将 json 序列化了,但是将它传递回客户端的代码是什么? (对不起 asp.net 的新手...)
    • 你会做一个简单的 ajax 调用,大多数 JS 框架让这个任务非常容易,例如 jQuery 有 $.ajax 方法。至于客户端应该调用什么,我将创建一个 asp.net 通用 HttpHandler,然后它可以从数据库中获取数据并通过响应流中的 JSON 将其发送回客户端。
    • 好的,越来越近了,但现在我遇到了一个错误。发表在上面^^对此有什么想法吗?
    • 从外观上看,您从数据库中获得的结果数量似乎少于 10。请尝试使用 Take(10) 代替 GetRange
    【解决方案2】:

    从 C# 中导出数据的最简单方法可能不是在 JavaScript 中使用数据的最简单方法,但我会从后者的角度对其进行尝试。

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

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

    【讨论】:

    【解决方案3】:

    如果您使用的是 asp.net mvc,那么您可以使用 javascript/jquery 调用将 JSON 返回给客户端。以下是 MVC 的一位大专家 (Phil Haack) 关于如何做到这一点的提示: Callng MVC from Javascript

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

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

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

    ASP.NET Web Methods & jquery

    【讨论】:

      猜你喜欢
      • 2021-01-25
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 2011-01-13
      • 2010-10-21
      • 1970-01-01
      相关资源
      最近更新 更多