Index was outside the bounds of the array in DbGeographyModelBinder

Jun 12, 2013 at 9:56 PM
Hi guys,

I am getting an error in the MVC 4 version when I try to host an event. The
DbGeographyModelBinder methos throws an 'Index outside of bounds of array' in the following controller method:
    public class DbGeographyModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            if (valueProviderResult != null)
            {
                string[] latLongStr = valueProviderResult.AttemptedValue.Split(',');
           //line below throws the error
                __string point = string.Format("POINT ({0} {1})", latLongStr[1], latLongStr[0]);__
                //4326 format puts LONGITUDE first then LATITUDE
                DbGeography result = DbGeography.FromText(point, 4326);
                return result;
            }
            return null;
        }
    }
Any ideas?

Thanks, Steve
Jul 3, 2013 at 6:14 PM
Edited Jul 3, 2013 at 6:15 PM
You're not the only one. The AttemptedValue for "Location" is coming back null. I am trying to pinpoint the problem, but it's going to take me a bit, while I navigate... Any help would be appreciated. Thanks.
Jul 27, 2013 at 1:00 AM
I have the same issue. Does the map load correctly on the page on GET Dinner/Create for you? Because it does not for me - I get the NaaN error.
Jul 29, 2013 at 10:00 PM
The problem is because the blur method is supposed to fire and bring back the Lat and Long. IT DOES NOT. Therefore a Null is passed and... the error. I haven't fixed this issue as yet. I have instead used a different service that was outlined in the walkthrough using the different Geolocation Services on MSDN in my dinner CREATION. Hope this helps... and if anyone figures out why the Blur function is not working in the JavaScript... let us know.
Nov 4, 2013 at 11:58 PM
The problem seems to originate in the Nerddinner.js file. I changed the following lines and my deployed site now works fine:
NerdDinner._callbackForLocation = function (result) {
    ZoomMap(result);

    if (result &&
           result.resourceSets &&
           result.resourceSets.length > 0 &&
           result.resourceSets[0].resources &&
           result.resourceSets[0].resources.length > 0) {
        // Add a pushpin at the found location
        var location = new Microsoft.Maps.Location(result.resourceSets[0].resources[0].point.coordinates[0], result.resourceSets[0].resources[0].point.coordinates[1]);
        var pushpin = new Microsoft.Maps.Pushpin(location);
        NerdDinner._map.entities.push(pushpin);

        $("#Location").val(location.latitude.toString() + "," + location.longitude.toString());
        $("#Latitude").val(location.latitude.toString());
        $("#Longitude").val(location.longitude.toString());
    }
};
Notice that _ZoomMap is not called correctly. I changed the code to this:
NerdDinner._callbackForLocation = function (result) {
    NerdDinner._ZoomMap(result);

    if (result &&
           result.resourceSets &&
           result.resourceSets.length > 0 &&
           result.resourceSets[0].resources &&
           result.resourceSets[0].resources.length > 0) {
        // Add a pushpin at the found location
        var location = new Microsoft.Maps.Location(result.resourceSets[0].resources[0].point.coordinates[0], result.resourceSets[0].resources[0].point.coordinates[1]);
        var pushpin = new Microsoft.Maps.Pushpin(location);
        NerdDinner._map.entities.push(pushpin);

        $("#Location").val(location.latitude.toString() + "," + location.longitude.toString());
        $("#Latitude").val(location.latitude.toString());
        $("#Longitude").val(location.longitude.toString());
    }
};
Nov 6, 2013 at 3:33 AM
Good work, that did it for me, thanks.