Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t使用JSON将对象列表传递给视图_Javascript_C#_Json_Ajax_Asp.net Mvc - Fatal编程技术网

Javascript Can';t使用JSON将对象列表传递给视图

Javascript Can';t使用JSON将对象列表传递给视图,javascript,c#,json,ajax,asp.net-mvc,Javascript,C#,Json,Ajax,Asp.net Mvc,使用AJAX调用将数据从控制器传递到视图 但由于某些原因,我无法使用JavaScript检索它 数据显示空对象的列表 public class Ville { string wilPop { get; set; }// = null; int identifiant { get; set; }// = -1; bool affectedD { get; set; } //= false; public Ville(string ewilPop, int eide

使用AJAX调用将数据从控制器传递到视图 但由于某些原因,我无法使用JavaScript检索它 数据显示空对象的列表

public class Ville {
    string wilPop { get; set; }// = null;
    int identifiant { get; set; }// = -1;
    bool affectedD { get; set; } //= false;

    public Ville(string ewilPop, int eidentifiant, bool eaffectedD)
    {
        this.wilPop = ewilPop;
        this.identifiant = eidentifiant;
        this.affectedD = eaffectedD;
    }

}
[Authorize]
[HttpPost]
public ActionResult GetWilByRegion(int? Wil)
{
    if (Wil != null)
    {
        string wilPop = null;
        int identifiant = -1;
        bool affectedD = false;

        //Get info from DB
        List<city> ListWil = new List<city>();
        ListWil = db.city.Where(m => m.idReg == Wil).OrderByDescending(m => m.population).ToList();
        List<Ville> mylist = new List<Ville>();

        foreach (city item in ListWil)
        {
            // Description in the DB
            wilPop = item.city + " (" + item.population + ")"; 
            // id value in the DB
            identifiant = item.city_id; 
            // if checked or not
            affectedD = CheckifWilAffected(item.city_id); 
            mylist.Add(new Ville(wilPop, identifiant, affectedD)); 
        }
        return Json(mylist, "Ville");

    }
    else return Json("Error");
}


由于
Ville
类没有任何public属性,因此将获得一个空对象数组(没有任何属性)

您需要将您的属性
公开

public class Ville
{
    public string wilPop { get; set; }
    public int identifiant { get; set; }
    public bool affectedD { get; set; }

    public Ville(string ewilPop, int eidentifiant, bool eaffectedD)
    {
        this.wilPop = ewilPop;
        this.identifiant = eidentifiant;
        this.affectedD = eaffectedD;
    }
}
现在,JavaScript序列化程序将能够在创建列表的JSON字符串表示形式时使用这些属性的值


我个人喜欢使用pascalCasing作为类属性名。(
WilPop
而不是
WilPop

你得到的是一个空对象数组(没有任何属性),因为你的
Ville
类没有任何公共属性

您需要将您的属性
公开

public class Ville
{
    public string wilPop { get; set; }
    public int identifiant { get; set; }
    public bool affectedD { get; set; }

    public Ville(string ewilPop, int eidentifiant, bool eaffectedD)
    {
        this.wilPop = ewilPop;
        this.identifiant = eidentifiant;
        this.affectedD = eaffectedD;
    }
}
现在,JavaScript序列化程序将能够在创建列表的JSON字符串表示形式时使用这些属性的值


我个人喜欢使用pascalCasing作为类属性名。(
WilPop
而不是
WilPop

请回答您的问题,并将
Ville
类附加调试器发布到控制器。它正在返回数据吗?在Fiddler中观看Ajax调用。回复是否具有预期的格式和内容?将调试器附加到javascript(Chrome工具)。
data
里面有什么吗?我的列表包含所有信息,但是当我将调试器附加到java脚本数据时,数据是空的(列表{},{},{},{},{},{},{},{},{}),请回答您的问题并将
Ville
classAttach调试器发布到控制器上。它正在返回数据吗?在Fiddler中观看Ajax调用。回复是否具有预期的格式和内容?将调试器附加到javascript(Chrome工具)。
data
中有任何内容吗?mylist包含所有信息,但是当我将调试器附加到java脚本时,数据是空的(列表{}、{}、{}、{}、{}、{}、{}、{})