asp.net mvc 3 - Passing values to Controller via Javascript return View MVC3 Razor -



asp.net mvc 3 - Passing values to Controller via Javascript return View MVC3 Razor -

i brand new mvc. trying pass longitude , latitude values obtain using geolocation controller can utilize values identify , pull right info database.

here javascript

function auto_locate() { alert("called station"); navigator.geolocation.getcurrentposition(show_map); function show_map(position) { var latitude = position.coords.latitude; var longitude = position.coords.longitude; var locstring = latitude.tostring() + "." + longitude.tostring(); var postdata = { latitude: latitude, longtitude: longitude } alert(locstring.tostring()); } }

all of works fine;

now need pass postdata or locstring controller. looks this:

[httpget] public actionresult autolocate(string longitude, string latitude) { new mynamespace.areas.mobile.models.geo { latitude = convert.todouble(latitude), longitude = convert.todouble(longitude) }; // work here set view info then... homecoming view(); }

i have searched , researched , have not been able find solution.

how can phone call javascript above html.actionlink , longitide , latitude controller?

you utilize ajax:

$.ajax({ url: '@url.action("autolocate")', type: 'get', data: postdata, success: function(result) { // process results controller } });

where postdata = { latitude: latitude, longtitude: longitude };.

or if had actionlink:

@html.actionlink("foo bar", "autolocate", null, null, new { id = "locatelink" })

you ajaxify link this:

$(function() { $('#locatelink').click(function() { var url = this.href; navigator.geolocation.getcurrentposition(function(position) { var latitude = position.coords.latitude; var longitude = position.coords.longitude; var postdata = { latitude: latitude, longtitude: longitude }; $.ajax({ url: url, type: 'get', data: postdata, success: function(result) { // process results controller action } }); }); // cancel default redirect link returning false homecoming false; }); });

javascript asp.net-mvc-3 razor

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -