【问题标题】:can't view the result of a query that i returned with json无法查看我使用 json 返回的查询结果
【发布时间】:2015-09-07 18:09:12
【问题描述】:

这是我在控制器中获取产品 ID 并使用 json 返回供应商的方法:

public ActionResult GetProductData(int ProductId)
            {
                var data = from m in db.Products
                      join sa in db.SupPro on m.ProductID equals sa.ProductID
                      join f in db.Supplier on sa.CompanyID equals f.CompanyID
                      where m.ProductID == ProductId
                      select new { CompanyName = f.NameS, AdressCompany = f.Address, PhoneCompany = f.Phone };
                return Json(new { foo = data.ToList(), ball = "dragon", elementId = ProductId }, JsonRequestBehavior.AllowGet);
            }

屏幕上的输出是:data res :[object Object] dragon 4

这些是我的模型类:

客户模型:

 public class Customer

{
    [Key]
    public int CustomerID { get; set; }
    public String NameS { get; set; }
    public String NameP { get; set; }
    public String Name { get; set; }
    public String Phone { get; set; }
    public String Address { get; set; }
    public String Email { get; set; }

    public virtual ICollection<SupPro> SupPro { get; set; }
}

供应商类:

   public class Supplier
        {
            [Key]
            public int CompanyID { get; set; }
            public String NameS { get; set; }
            public String Address { get; set; }
            public String Phone { get; set; }

            public virtual ICollection<SupPro> SupPro { get; set; }
        }

产品类别:

public class Products
    {
        [Key]
        public int ProductID { get; set; }
        public String NameP { get; set; }
        public decimal Price { get; set; }
        public int Quantity { get; set; }

        public virtual ICollection<SupPro> SupPro { get; set; }
    }

和 supPro 类:

public class SupPro
    {
        [Key]
        public int SupProID { get; set; }
        public int CustomerID { get; set; }
        public int ProductID { get; set; }
        public int CompanyID { get; set; }
        public DateTime SupplyDate { get; set; }

        public virtual Products Product { get; set; }
        public virtual Supplier Supplier { get; set; }
        public virtual Customer Customer { get; set; }
    }

谁能告诉我我的问题是什么,这样我就可以根据需要查看查询的结果。

谢谢。

【问题讨论】:

  • 显示客户端代码!
  • 你不能直接print json数据到浏览器。您需要迭代 json 对象并使用各个属性。否则,您需要使用某种绑定库,如 knockout.js 来为您处理客户端 json 与 html 的绑定。

标签: c# asp.net json asp.net-mvc asp.net-mvc-4


【解决方案1】:

我不确定这是否有帮助,但如果您有一个包含 JSON 类或数组的 JavaScript 变量,并且如果您使用的是 AngularJS,那么以可读形式显示该值真的很容易:

<pre> {{ myArrayOfObjects | json }} </pre>

我经常使用这个技巧,尤其是在编写 AngularJS 代码以及测试我的控件是否绑定到 JSON 对象中的正确字段时。

但是是的,你是对的,没有 AngularJS,试图显示这样一个变量的内容只会返回一个毫无意义的结果:

[object Object]

【讨论】:

    【解决方案2】:

    您的 json 可以在 jQuery successdone 调用中访问,如下所示:

    foo 是一个集合,因此可以通过索引或循环访问它,然后是属性名称。

    以获取第一个CompanyName属性为例。

    data.foo[0].CompanyName
    

    更常见的是,您会使用循环来使用 jQuery 输出这些内容,您可以使用 $.each 并将它们附加到元素中。

    您定义的其他元素可通过以下方式访问:

    data.ball
    data.elementId
    

    更新

     success: function (data) {
                                var x = data;
    
                                $.each(data.foo, function (i, item) {
                                   $nextElm.append(item.CompanyName + '<br/>');
                                });
    
                             }
    

    显然将附加更改为您想要的输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多