我有销售和销售项目实体,我试图使用asp.net core 2.0中的return boto3.client()
函数将一些json从控制器返回到前端
这是我的控制器代码:
json ()
在前面,使用jQuery ajax get call:
[HttpGet]
public JsonResult GetSales(int saleId)
{
SalesModel sales = saleRepository.All().FirstOrDefault(x => x.Id == saleId);
sales.Items = saleItemRepository.All().Where(x => x.SalesId ==
saleId).ToList();
return Json(sales);
}
控制台上的我得到json,这是无效的:数组结尾}]}丢失
function getSale(id) {
return $.ajax({
type: 'GET',
url: "/Sales/GetSales",
data: "saleId=" + id
});
}
$.when(getSale(saleId)).then(function (res) {
console.log(res);
}).fail(function (err) {
console.log(err);
});
但是当我对salesItem进行硬编码时,它返回了有效的json并且似乎没问题
{"salesDate":"2018-06-14T00:00:00","saleCode":"1527863276313","notes":"Ok","total":12.31,"status":"Due","discount":0.31,"grandTotal":12.0,"paymentMethod":"Cash","customerId":3,"customerModel":null,"items":[{"name":"hello","price":2.33,"quantity":4,"amount":9.32,"salesId":7
从数据库中获取有效的json时我缺少什么?这个代码在aspnetmvc 5上完美运行
答案 0 :(得分:0)
尝试更新您的代码:
[HttpGet]
public IActionResult GetSales(int saleId)
{
SalesModel sales = saleRepository.All().FirstOrDefault(x => x.Id == saleId);
sales.Items = saleItemRepository.All().Where(x => x.SalesId ==
saleId).ToList();
return OK(sales);
}
无需使用Json方法显式转换销售对象。另外,注意到我已将返回类型更改为IActionResult
并返回OK
响应。