3

I have an ASP.NET MVC controller action 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;
});
0

2 Answers 2

1

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?

1

You need the JsonValueProviderFactory.

Sending JSON to an ASP.NET MVC Action Method Argument

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.