title: How to get latitude and longitude of an address and save to database
body: <p>I have a html form where the user enters his address (geo location - street, city, and so on). Now i want to retrieve the latitude and longitude of that address when the user clicks submit button. My code looks like this:
  View:</p>
  
  <pre><code> @using (Html.BeginForm(new { onsubmit = "return getCoordinates();" }))
  {
      &lt;script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
          function getCoordinates() {
              var geocoder = new google.maps.Geocoder();
              var address = document.getElementById('@Html.Id("address")').value;
  
              geocoder.geocode({ 'address': address }, function (results, status) {
  
                  if (status == google.maps.GeocoderStatus.OK) {
                      var latitude = results[0].geometry.location.lat;
                      var longitude = results[0].geometry.location.long;
                      alert(latitude);
                    document.getElementById('@Html.Id("latitude")').value = latitude;
                      document.getElementById('@Html.Id("longitude")').value = longitude;
                  }
              });
  }
     &lt;/script&gt;
     @Html.ValidationSummary(true, "Please correct the errors and try again")
  
      @Html.EditorFor(model =&gt; model.latitude)
      @Html.EditorFor(model =&gt; model.longitude)
        &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.address)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
          @Html.TextBoxFor(model =&gt; model.address)
          @Html.ValidationMessageFor(model =&gt; model.address)
          &lt;/div&gt;
        }
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>        public class RentOutSpace
          {
          [HiddenInput(DisplayValue = false)]
          public string latitude { get; set; }
          [HiddenInput(DisplayValue = false)]
          public string longitude { get; set; }
          public string address { get; set; }
          }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>              [HttpPost]
                public ActionResult AddSpace(RentOutSpace rentModel)
                {
              Session.Store(rentModel);
              Session.SaveChanges();
              return View();
                }
  </code></pre>
  
  <p>My question is how can i pass the field value of the address to my JavaScript function and pass the latitude and longitude variables values to my model, so i can save them in a database?</p>