从控制器发送阵列中的MVC使用JSON视图 [英] send array from controller to a view using JSON in MVC

查看:111
本文介绍了从控制器发送阵列中的MVC使用JSON视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code:

var mapcity = new Array([]);              

         $.ajax({
             type: 'GET',
             url: '/home/DrawMap',
             dataType: 'json',
             success: function (data) {

                 var len = data.length;

                 for (var i = 0; i < len; i++) {

                    // mapcity['' + data[i].name + ''] = { center: new google.maps.LatLng(data[i].x, data[i].y), population: data[i].population, name: '' + data[i].name + '' };
                     mapcity['' + data[i].name + ''] = { center: new google.maps.LatLng(data[i].x, data[i].y), population: data[i].population, name: ''+data[i].name+'' };
                     //newarr[i] = data[i].name;

                   alert(mapcity[0].population)
                 }
                 }
         });
}

这是我的code的一部分,这是从控制器内的功能:

This is a part of my code, and this is the function from controller :

public ActionResult DrawMap() {   

  string data = "[{ 'x':31.949454,'y': 35.932913,'population':50000,'name':'amman'},{ 'x':33.79,'y': 33.39,'population':100000,'name':'Zarqa'}]";
        data=data.Replace("'","\"");
        return this.Content(data, "application/json");       

    }

当我运行它,它的HET从控制器,但没有保存到mapcity变量JSON数据!
它什么都不做。我怎么能解决这个问题,什么IAM做错了吗?

When I run this, its het the JSON data from the controller but without saving it into the mapcity variable! And it does nothing. how can I solve it and what iam doing wrong?

推荐答案

您不应该手动构建JSON你没有,但总是用JSON序列化:

You should never be manually building JSON as you did but always use a JSON serializer:

public ActionResult DrawMap() 
{
    var data = new[]
    {
        new
        {
            x = 31.949454,
            y = 35.932913,
            population = 50000,
            name = "amman",
        },
        new
        {
            x = 33.79,
            y = 33.39,
            population = 100000,
            name = "Zarqa",
        }
    };

    return Json(data, JsonRequestBehavior.AllowGet);
}

你也已经宣布了成功处理程序这是导致我到以为你试图将$就调用后立即使用它的价值以外的 mapcity 变量,显然是不可能的,因为AJAX就其自己的本性是异步的。这意味着,成功回调可能很久以后执行,$就调用立即返回。

Also you have declared your mapcity variable outside of the success handler which is leading me into thinking that you are attempting to use its value immediately after the $.ajax call which obviously is not possible because AJAX by its very own nature is asynchronous. This means that the success callback could be executed much later and the $.ajax call returns immediately.

因此​​,要使用这个变量的唯一一个安全的地方就是成功的功能:

So the only single safe place to use this variable is INSIDE the success function:

$.ajax({
    type: 'GET',
    url: '/home/DrawMap',
    success: function (data) {
        var mapcity = {};
        var len = data.length;
        for (var i = 0; i < len; i++) {
            mapcity['' + data[i].name + ''] = { center: new google.maps.LatLng(data[i].x, data[i].y), population: data[i].population, name: ''+data[i].name+'' };
        }
    }
});

您也有另外一个问题。您声明 mapcity 变量数组(新的Array([])),然后您试图插入非整数索引进去:

You also have another problem. You declared mapcity variable as array (new Array([])) and then you attempted to insert non-integer indexes into it:

mapcity['' + data[i].name + '']

在JavaScript的,当你声明一个数组,它只能有0基于整数索引。所以,你既可以其声明为JavaScript对象变种 mapcity = {}; 或者用整只索引

In javascript when you declare an array, it can only have 0 based integer indexes. So you could either declare it as a javascript object var mapcity = {}; or use integer indexes only.

这篇关于从控制器发送阵列中的MVC使用JSON视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆