我有一个Action,我使用它来使用GeoJson返回GeoJson.Net数据。行动方法如下:
public ActionResult GetWorldGeometry()
{
var worldgeo = (from w in _bdEntities.worlds1 select w);
foreach (var w in worldgeo )
{
var world = SqlGeometry.STGeomFromText(new SqlChars(w.geom.AsText()), 4326);
var worldGeometry = world.ToGeoJSONGeometry();
var worldFeatureProperties = new Dictionary<string, object>
{
{"Name", w.NAME},
{"Population(05)", w.POP2005},
{"Region", w.REGION},
{"ISO2", w.ISO2},
{"ISO3", w.ISO3},
{"Area", w.AREA}
};
var feature = new Feature(worldGeometry, worldFeatureProperties);
var serializedData = JsonConvert.SerializeObject(feature, Formatting.Indented,
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
});
return Content(serializedData, "application/json");
}
return null;
}在我看来,我想将这个GeoJson绘制成传单或OpenLayers,但是我目前正在使用下面的代码测试Action方法的输出:
<div>
<p>
<span id="add_here"></span>
</p>
</div>
@section scripts
{
<script>
$.ajax({
url: '@Url.Action("GetJsonResult", "Home")',
dataType: "json",
type: "GET",
contentType: 'application/json; charset=utf-8',
cache: false,
data: {},
traditional: true,
success: function(data) {
var reslen = data.length;
alert(reslen);
var result = JSON.stringify(data);
alert(result);
$('#add_here').text(result);
},
error: function(xhr) {
alert('error');
}
});
</script>
}我没有得到一个数据数组,而是得到了第一个警告结果长度的result.On,它写着“未定义”。在我的视图中,如何获得由Action方法中的查询返回的所有数据?下面是返回的单个结果的片段和警告消息:输出结果
发布于 2015-12-12 00:12:45
这是你什么,我希望它会成功,没有测试出来。
public class WorldFeatureProperties
{
public string Name { get; set; }
public string Population05 { get; set; }
public string Region { get; set; }
public string ISO2 { get; set; }
public string ISO3 { get; set; }
public string Area { get; set; }
public string WorldGeometry { get; set; }
}
public ActionResult GetWorldGeometry()
{
var worldgeo = (from w in _bdEntities.worlds1 select w);
var retval = new List<WorldFeatureProperties>();
foreach (var w in worldgeo)
{
var currentLocation = JsonConvert.DeserializeObject<WorldFeatureProperties>(w);
currentLocation.WorldGeometry = WorldGeometry.STGeomFromText(new SqlChars(w.geom.AsText()), 4326).ToGeoJSONGeometry();
retval.Add(currentLocation);
}
var serializedData = JsonConvert.SerializeObject(retval, Formatting.Indented,
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
});
return Json(serializedData, JsonRequestBehavior.AllowGet);
}https://stackoverflow.com/questions/34234326
复制相似问题