I have a change/getJson function that looks like this:
$('#CountryId').change(function () {
$.getJSON('@Url.Action("StateList", "Manage")', {id: $('#CountryId').val()}, function (data) {
var items = '<option value="">Select a State</option>';
$.each(data.List, function (i, state) {
items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
});
$('#StateId').html(items);
//Fail
//console.log(">>>>" + data.LatLng.Latitude);
//$('#Latitude').val(data.LatLng.Latitude);
//Success
//$.each(data.LatLng, function (i, country) {
// $('#Latitude').val(country.Latitude);
// console.log(">>>>" + country.Latitude);
//});
$('#CityId').html('<option value="">Select a City</option>');
});
});
The data is coming back from my json result fine. It's returning a statelist for that CountryId (CountryId is FK from the States table) to build my selectlist/dropdownfor control. It is also returning the Latitude and Longitude for the CountryId (From the Countries table). The State list for my selectlist is fine. However, the Latitude and Longitude will only ever be one set of values (i.e. one record). The only way I can get it to work in jquery is with the .each
loop:
$.each(data.LatLng, function (i, country) {
$('#Latitude').val(country.Latitude);
console.log(">>>>" + country.Latitude);
});
I don't need a loop though since it's a single set of values. Is there another, cleaner way to format this and avoid the extra function? As you can see from my code above, I've tried this:
console.log(">>>>" + data.LatLng.Latitude);
$('#Latitude').val(data.LatLng.Latitude);
...and many other things and it always comes back undefined. Here is my StateList piece from the controller:
[HttpGet]
public ActionResult StateList(int id)
{
var list = db.States.Where(d => d.CountryId == id).Select(d => new { Text = d.StateName, Value = d.StateId }).ToList();
var latlng = db.Countries.Where(d => d.CountryId == id).Select(d => new { d.Latitude, d.Longitude });
var result = new { List = list, LatLng = latlng };
return Json(result, JsonRequestBehavior.AllowGet);
}
I tried formatting the latlng
data in this ActionResult (i.e. ToList
, ToString
, Lat = d.Latitude
, etc etc), yet jquery doesn't seem to care as long as I put it in the loop.
All examples that I can find on the internet are for json lists which worked great on the state list section. But, I can't seem to find anything regarding a single json value. And it seems thus far, that you have to break the value into pieces so jquery can interpret it? Anyone know the trick to eliminate the loop?