javascript - 通过Javascript返回值将值传递给Controller View MVC3 Razor

原文 标签 javascript asp.net-mvc-3 razor

Passing values to Controller via Javascript return View MVC3 Razor

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?

Answer

You 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;
    });
});

翻译

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

这是我的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());

}

}


所有这些都很好。

现在,我需要做的是将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();
}


我已经进行了搜索和研究,但找不到解决方案。

如何从HTML.ActionLink调用上述javascript,并将Longitide和Latitude传递到控制器?
最佳答案
您可以使用AJAX:

$.ajax({
    url: '@Url.Action("AutoLocate")',
    type: 'GET',
    data: postData,
    success: function(result) {
        // process the results from the controller
    }
});


其中postData = { latitude: latitude, longtitude: longitude };

或者,如果您有一个动作链接:

@Html.ActionLink("foo bar", "AutoLocate", null, null, new { id = "locateLink" })


您可以像这样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;
    });
});
相关推荐

javascript - $('div,span',$('。test'))。selector输出什么? jQuery的错误?

javascript - 封装的Javascript函数检查变量是否未定义?

javascript - iOS Web应用程序:在输入文本字段上禁用自动对焦

javascript - 数组默认没有原型吗?

javascript - 将extjs drop事件监听器放在控制器而不是视图中

javascript - Google Chrome扩展程序中的自动对焦

javascript - jQuery Validator —自定义方法有效时不会隐藏错误

javascript - 查找和删除文本,然后使用jquery或javascript插入图像

javascript - 如何使用带有1个参数的jquery函数附加图像?

javascript - 从源URL获取转发的URL?