1

只是我,还是真的很难在 javascript 中使用变量?

我的问题是我想将我的 javascript 函数的一些结果用于 MVC .net 控制器,但我不能!

1.- 我的 JavaScript 函数在视图上运行:

     $(document).ready(function () {
           $('#go').click(function () {
             // test for presence of geolocation
             if (navigator && navigator.geolocation) {
                 navigator.geolocation.getCurrentPosition(geo_success, geo_error);

             } else {
                 // nothing at this moment.

……

   function geo_success(position) {
         printLatLong(position.coords.latitude, position.coords.longitude);
     }

除了 printLatLong 函数,我想使用参数 position.coord.latitude 通过 Html.ActionLink 将其发送到控制器,如下所示:

    @Html.ActionLink("Link to Controller", "Action", "Controller", position.coords.latitude)

但是我不能使用 position.coords.latitude ,因为现在我已经退出了这个功能。我只想在我的 ActionLink 中使用该值作为参数,这太难了吗?:(

提前感谢您的帮助。

4

1 回答 1

1

只需使用 html 锚点并通过 jquery 操作 href 属性即可。

脚本:

function geo_success(position) {
    printLatLong(position.coords.latitude, position.coords.longitude);
    var url = '@Url.Action("Geo", "Home")';
    // grab the anchor and modify the url
    $("#geolink").attr("href", url += "?latitude=" + position.coords.latitude);
}

在你看来

<a id="geolink">GeoLink</a>

家庭控制器.cs

    public ActionResult Geo(int latitude)
    {
        throw new Exception(String.Format("Do something with: {0}", latitude));
    }
于 2012-09-05T09:26:58.193 回答