0

In my HomeController I have the following method:

public JsonResult AjaxTest(Position postData)
{
        Session["lat"] = postData.Lat;
        Session["lng"] = postData.Long;

    return Json("", JsonRequestBehavior.AllowGet);
}

How can I have lat and lng in my Index method?

It is public async Task<ActionResult> Index() , if it matters.

The script in the view that is retrieving and passing the user's current coordinates:

var x = document.getElementById("positionButton");

(function getLocation() 
{
    if (navigator.geolocation) 
    {
        navigator.geolocation.getCurrentPosition(showPosition);
    }
}());

function showPosition(position) 
{
    if (position == null) alert('Position is null');
    if (position.coords == null) alert('coords is null');

    $('#lat').text(position.coords.latitude);
    $('#long').text(position.coords.longitude);

    var postData = { Lat: position.coords.latitude, Long: position.coords.longitude };

    $.ajax(
    {
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "@Url.Action("AjaxTest", "Home")",
        //dataType: "json",
        data: JSON.stringify(postData)

    });
}

I need to be able to have the user's current lat/long to get the distance to another position. lat and lng are declared as public variables, so why do they stay 0 when trying to use the coordinates inside the Index method?

Edit, here is the Index method:

public object x;
     public async Task<ActionResult> Index()
            {


                x = Session["lat"];


                return View(parkingLot);
            }
crystyxn
  • 1,411
  • 4
  • 27
  • 59
  • Are you accessing them in the same request? – JB06 Dec 05 '16 at 18:50
  • I am not sure, after Index loads and gets the coordinates, it sends it to the JsonResult. – crystyxn Dec 05 '16 at 18:52
  • Try passing postData as an object, not a string. data: postData. Another thing to check... Have you set a debugger on your javascript to make sure your Lat and Long are not 0 there? – Daryl Dec 05 '16 at 18:53
  • I made that change you said, what do I have to modify in the method? No I did not set it – crystyxn Dec 05 '16 at 18:59

2 Answers2

1

If the variables are declared in your controller, that could explain why they are zero.

Controllers are created on a per-request basis. So if you hitting AjaxTest and setting the lat/long, when the JsonResult gets returned to the ajax call, the controller with your variables is disposed. Try using Session instead of variables. See here.

JB06
  • 1,881
  • 14
  • 28
  • So I changed it to Session["lat"] = postData.Lat; and then in the Index controller I use x = Session["lat"]; ? (public object x) because it doesnt work Not sure what im doing wrong – crystyxn Dec 05 '16 at 19:12
  • I put a breakpoint but x remains null, I did something wrong I think – crystyxn Dec 05 '16 at 19:16
  • In the AjaxTest action, is postData null? – JB06 Dec 05 '16 at 19:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129833/discussion-between-jb06-and-crystyxn). – JB06 Dec 05 '16 at 19:42
  • I can't use chat here at work, I can help more once I leave if you haven't figured it out already. – JB06 Dec 05 '16 at 19:44
0

You can use TempData to put your data in AjaxTest action like this:

public ActionResult AjaxTest(Position postData)
{
        this.TempData["lat"] = postData.Lat;
        this.TempData["lng"] = postData.Long;

    return RedirectToAction("Index");
}

And in index action you can retrieve your data like this:

public async Task<ActionResult> Index()
{
    var lat = this.TempData["lat"];
    var lng = this.TempData["lng"];
    return View(parkingLot);
}

When to use TempData vs Session in ASP.Net MVC

Community
  • 1
  • 1
TotPeRo
  • 6,561
  • 4
  • 47
  • 60