问题:
(your query).FirstOrDefault();
返回一个元素而不是列表.
Json(x)
将x转换为包含x的json表示的字符串.
以下是您的选择:
如果要返回对象列表,可以删除FirstOrDefault()
并使用return (your query).ToList()
.
您可以将方法的返回类型更改为Detail
而不是List
使用return orderDetails;
而不是 使用return Json(orderDetails);
更新:
从你想要"在MVC控制器中的json中返回列表"的问题的标题推断
您应该将方法更改为此
public ActionResult GetOrderDetails() { var orderDetails = (from user in db.Users join detail in db.OrderDetails on user.Id equals detail.UserId where (detail.DateAdded != null) select new Detail { FirstName = user.FirstName, LastName = user.LastName, Origin = detail.OriginCode, Destination = detail.DestinationCode, CarrierCode = detail.CarrierCode, IsReturn = detail.IsReturn, IsCancel = detail.IsCancel, OrderId = detail.OrderId }).ToList(); return Json(orderDetails, JsonRequestBehavior.AllowGet); }