admin管理员组文章数量:1312658
I am brand new to MVC. I am trying to pass longitude and latitude values I obtain using geolocation to my controller so that I can use the values to identify and pull the correct data from my database.
Here is my Javascript
function auto_locate() {
alert("called from 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 this works fine;
Now what I need to do is pass postData or locstring to my controller. Which looks like this:
[HttpGet]
public ActionResult AutoLocate(string longitude, string latitude)
{
new MyNameSpace.Areas.Mobile.Models.Geo
{
Latitude = Convert.ToDouble(latitude),
Longitude = Convert.ToDouble(longitude)
};
// Do some work here to set up my view info then...
return View();
}
I have searched and researched and I have not been able to find a solution.
How can I call the javascript above from an HTML.ActionLink and get the Longitide and Latitude to my controller?
I am brand new to MVC. I am trying to pass longitude and latitude values I obtain using geolocation to my controller so that I can use the values to identify and pull the correct data from my database.
Here is my Javascript
function auto_locate() {
alert("called from 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 this works fine;
Now what I need to do is pass postData or locstring to my controller. Which looks like this:
[HttpGet]
public ActionResult AutoLocate(string longitude, string latitude)
{
new MyNameSpace.Areas.Mobile.Models.Geo
{
Latitude = Convert.ToDouble(latitude),
Longitude = Convert.ToDouble(longitude)
};
// Do some work here to set up my view info then...
return View();
}
I have searched and researched and I have not been able to find a solution.
How can I call the javascript above from an HTML.ActionLink and get the Longitide and Latitude to my controller?
Share Improve this question edited Jul 11, 2011 at 20:17 SLaks 888k181 gold badges1.9k silver badges2k bronze badges asked Jul 11, 2011 at 20:15 Dylan JonesDylan Jones 331 gold badge1 silver badge3 bronze badges1 Answer
Reset to default 5You could use AJAX:
$.ajax({
url: '@Url.Action("AutoLocate")',
type: 'GET',
data: postData,
success: function(result) {
// process the results from the controller
}
});
where postData = { latitude: latitude, longtitude: longitude };
.
Or if you had an actionlink:
@Html.ActionLink("foo bar", "AutoLocate", null, null, new { id = "locateLink" })
you could AJAXify this link like 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 the results from the controller action
}
});
});
// cancel the default redirect from the link by returning false
return false;
});
});
本文标签: aspnet mvc 3Passing values to Controller via Javascript return View MVC3 RazorStack Overflow
版权声明:本文标题:asp.net mvc 3 - Passing values to Controller via Javascript return View MVC3 Razor - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741913034a2404558.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论