通过 Javascript 将值传递给控制器​​返回 View MVC3 Razor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6655978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:26:03  来源:igfitidea点击:

Passing values to Controller via Javascript return View MVC3 Razor

javascriptasp.net-mvc-3razor

提问by Dylan Jones

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.

我是 MVC 的新手。我正在尝试将我使用地理定位获得的经度和纬度值传递给我的控制器,以便我可以使用这些值来识别和从我的数据库中提取正确的数据。

Here is my Javascript

这是我的 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:

现在我需要做的是将 postData 或 locstring 传递给我的控制器。看起来像这样:

[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?

如何从 HTML.ActionLink 调用上面的 javascript 并将 Longitide 和 Latitude 发送到我的控制器?

回答by Darin Dimitrov

You could use AJAX:

你可以使用 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 };.

哪里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:

你可以像这样 AJAXify 这个链接:

$(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;
    });
});