如何遍历 @Model.ICollection in Java Script 函数
How to iterate over @Model.ICollection in Java Script function?
我有一个@Model.LoginCoordinates
LoginCoordinates
ICollection<Coordinates>
。
Coordinates
类:
public class Coordinates {
public int Id { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
}
在视图中,我有:
<script>
function f(){
for (var i = 0, n = @Model.LoginCoordinates.Count; i < n; i++) {
alert(i);//works fine, I just wanted to check if the loop works at all
}
}
</script>
它工作正常,但我想显示所有操作:
<script>
function f(){
for (var i = 0, n = @Model.LoginCoordinates.Count; i < n; i++) {
var latitute = @Model.LoginCoordinates[i].Latitude;
var longituted = @Model.LoginCoordinates[i].Longitude;
}
}
</script>
但是我无法访问LoginCoordinates
i
元素,因为它ICollection
.我也相信 foreach 是不可能的,因为它是 c# 对象。
问题:如何在JavaScript
内部迭代ICollection
?
编辑
以上是SSCCE,真正的代码是;
var map;
function InitializeMap() {
alert('works');
var lat = @Model.LoginCoordinates.First().Latitude;
var lon = @Model.LoginCoordinates.First().Longitude;
var latlng = new google.maps.LatLng(lat, lon);
var mapOptions =
{
zoom: 16,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
};
map = new google.maps.Map(document.getElementById("map"), mapOptions); //this breaks the script
}
window.addEventListener('load', InitializeMap);
这
应该使用Newtonsoft.json的JsonConvert
来完成
<script>
var coordinatesJson='@Html.Raw(JsonConvert.Serialize(Model.LoginCoordinates.ToArray())'
var coordinates=JSON.parse(coordinatesJson);
//you now have coordinates as javascript object
var map;
function InitializeMap() {
// could loop over if needed
for(var coords in coordinates) {
// do something with coords
}
</script>
怎么样
<script>
function f(){
var coordinates= @Model.LoginCoordinates.ToArray();
for (var i = 0, n = coordinates.Length; i < n; i++) {
var latitute = coordinates[i].Latitude;
var longituted = coordinates[i].Longitude;
}
}
</script>
模型只能在渲染视图时在服务器端访问。因此,为了在客户端访问其属性,您需要在 C# (razor) 中迭代集合并生成客户端 JavaScript 代码随后可以访问的 HTML。
另一种选择是创建一个控制器方法,该方法将模型作为 JSON 返回,并通过 AJAX 从客户端代码调用它。
就其价值而言,此语法可能对某人有用:
@section Scripts {
<script type="text/javascript">
@if (Model.LoginCoordinates.Any())
{
//C# loop over C# object
foreach (var point in Model.LoginCoordinates)
{
//We are mixing C# and JS code here
//you could call google's JS API, but still work with C# object properties
@:console.log(@point.Latitude, @point.Longitude);
}
}
</script>
}
相关文章:
- Node.js's Buffer.writeFloatBE in Javascript
- Setting default onclick behavior for <img> tag in gene
- Lucene Search in Alfresco
- grep in JQuery to C#
- write HTML in JavaScript
- jQuery setTimeOut: in for-loop
- SetTimeout and clearTimeout in Javascript
- lightbox in html 5 and javascript
- HTMLInputElement in IE7
- Mongodb$in以与数组中相同的顺序获取结果
- element.dataset in Internet Explorer
- jQuery parent() in table
- Instagram oauth flow in angularjs
- In循环的In运算符前后
- 如何使用for in循环在javascript中显示对象中的对象
- Jquery change onchange in onload
- 重构 jQuery in JS 问题
- strongloop script.js run find in before delete方法:如何同步运行异步方法
- TicTacToe minimax AI in Javascript
- 如何遍历 @Model.ICollection in Java Script 函数