如何将变量的值从javascript传递到mvc 2 asp.net变量

问题描述:

你好,

任何人都可以帮助我解决这个问题.我有一个javascript,其变量的值需要传递给asp.net mvc中的变量.

Hello,

Can anybody help me with this problem. I have an javascript the value of whose variable needs to be passed to variable in asp.net mvc.

<script type="text/javascript">
        var allMarks = [];
        google.load("maps", "2");
        var geocoder;
       var lat;
        function initialize() {
            var map = new google.maps.Map2(document.getElementById("map"));
            map.setCenter(new google.maps.LatLng(37.4419, -122.1419), 10);
            map.setUIToDefault();
            GEvent.addListener(map, "click", function (overlay, latlng) {
                if (latlng) {
                    var marker = new GMarker(latlng);
                    allMarks.push(latlng);
                    map.addOverlay(marker);
                    marker.openInfoWindow(latlng.toString());
                    lat = latlng.toString();
              }
            });
           
            geocoder = new google.maps.Geocoder();
        }
        google.setOnLoadCallback(initialize);

    </script>



这个脚本只是从谷歌地图获取纬度和经度,现在我需要将此纬度和经度值传递给asp.net mvc变量.我需要能够在mvc控制器的操作结果方法中捕获此值.以下是我的Mvc控制器操作结果.如何从javascript获取lat变量的值并将其传递给操作结果GetLatLng方法.我对asp.net以及mvc都很陌生..请帮助:(



This script just gets the latitude and longitude from google map,now i need to pass this latitude and longitude values to asp.net mvc variable. I need to be able to catch this value in a action result method of mvc controller. Below is my Mvc controller action result. How to get the value of lat variable from javascript and pass it to action result GetLatLng method. I am very new to asp.net as well mvc .. Please Help :(

public ActionResult GetLatLng(string Lat)
        {
            TempData["lat"] = Lat;
            return View("Index");
        }



Thnks



Thnks

我认为有一个隐藏的文本框来保存javascript中的值,并在您的MVC操作中访问它.我不确定这是否是最有效的方法,但它可能会解决问题.

HTH!

Dinesh
我不是ASP.NET MVC的专家,答案更多的是基于直觉,而不是知识.
I think having a hidden text box to hold the value from the javascript and accessing it on your MVC action should work. I''m not sure if it is the most effecient way to do it, but it just might do the trick.

HTH!

Dinesh
I''m not an expert in ASP.NET MVC and the answer is based more on intuition than knowledge.


在javascript中使用documnet.getElementbyId("idoftextbox").value = javascriptvarialbe;文本框现在获取分配给javascript变量的值.如果这是这种效果的方法,请不要.如果有更好的方法,请纠正我

谢谢
In javascript use documnet.getElementbyId("idoftextbox").value=javascriptvarialbe; textbox now gets the value assigned to javascript variable. Donno if this is the efficeint way of this. Please correct me if there is better way of doing it

Thank you