Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

JavaScript
<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>


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 :(

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


Thnks
Posted
Updated 24-Nov-11 2:12am
v2
Comments
manu g m 25-Nov-11 11:45am    
Thanks a lot for your reply. Ya ur rite can use hidden fields also the way which i found was to use document.getelementbyid("name").value=varialbevalue
Even i am not an expert, and even i donno whether this is the efficient way of doing it
Dinesh Mani 29-Nov-11 0:09am    
"document.getelementbyid" is how you access any control from javascript. When I said have a hidden text box to hold the value from javascript, this is exactly what I meant, i.e. you would access the hidden control say hdnText1 using "document.getelementbyid('hdnText1').value = varValue" and access the hdnText1 from the action code on the server side.

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.
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900