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>

HomeController.cs

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