Can't view the result of the request I returned with json

this is my method in the controller that gets the product id and returns the supllier using 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);
            }

      

the output on the screen was: res: object [object Object] dragon 4

these are my model classes:

customer model:

 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; }
}

      

provider class:

   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; }
        }

      

product class:

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; }
    }

      

and the supPro class:

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; }
    }

      

can someone tell me what my problem is, so i can view the query result as i want.

thank.

+3


source to share


2 answers


Your json will be available in a jQuery call success

or done

like this:

foo is a collection, so it will be available via indexing or looping and then the property name.

As an example, to get the first property CompanyName

.

data.foo[0].CompanyName

      

More often than not, you should use a loop to output this data with jQuery, which you can use $.each

and attach to an element.



Other elements that you define will be accessible via:

data.ball
data.elementId

      

Update

 success: function (data) {
                            var x = data;

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

                         }

      

Obviously change the addition to the desired result.

0


source


I'm not sure if this helps, but if you have a JavaScript variable containing a JSON class or array and if you are using AngularJS then it is really easy to display the value in a readable form:

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

      

I use this trick a lot, especially when writing AngularJS code and testing that my controls are bound to the correct fields in a JSON object.



enter image description here

But yes, you're right, without AngularJS, trying to display the contents of a variable like this will just return pointless:

[object Object]

      

0


source







All Articles