开发者

jQuery Ajax with ASP.NET MVC Action: Passing arguments from JavaScript in POST

I have an ASP.NET MVC controller actio开发者_如何学运维n with the following VB.NET signature:

<HttpPost()>
Public Function ClosestCities
   (ByVal position As MapCoordinate, ByVal citiesCount As UInteger) As JsonResult

The MapCordinate class is:

Public Class MapCoordinate
    Public Latitude As Double
    Public Longitude As Double
End Class

If I'm trying to send an Ajax POST in jQuery to the ClosestCities action, what should my request look like?

When I use the following code to POST to this action, in the debugger window of VS, position.longitiude and position.latitude are equal to 0.0 (0D):

$.post("/geodata/closest-cities", { 
    position: {
        Longitude: mapPosition.lng(),
        Latitude: mapPosition.lat()
    },
    citiesCount: 5
}, function (cities) {
    debugger;
});


I'm thinking your best bet may be to accept position as an Object in your prototype and use CType() to explicitly convert it to a MapCoordinate within your function. Implicit conversions don't always work the way we assume they should.

Alternatively, you may just change your MapCoordinate class to define Latitude and Longitude as Properties which I find tend to behave better for the purposes of serialization and assignment, especially on implicit conversion.

Have you tried this code yet? Does the implicit conversion produce any errors from the debugger?


You need the JsonValueProviderFactory.

Sending JSON to an ASP.NET MVC Action Method Argument

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜