【发布时间】: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; }
}
谁能告诉我我的问题是什么,这样我就可以根据需要查看查询的结果。
谢谢。
【问题讨论】:
-
显示客户端代码!
-
你不能直接
printjson数据到浏览器。您需要迭代 json 对象并使用各个属性。否则,您需要使用某种绑定库,如knockout.js来为您处理客户端 json 与 html 的绑定。
标签: c# asp.net json asp.net-mvc asp.net-mvc-4