当前位置: 移动技术网 > IT编程>开发语言>.net > MVC后台创建Json(List)前台接受并循环读取实例

MVC后台创建Json(List)前台接受并循环读取实例

2017年12月12日  | 移动技术网IT编程  | 我要评论
---------------------------后台-------------------
复制代码 代码如下:

[httppost]
public jsonresult checkstock(ienumerable<pvidscount> pvids)
{
var resultlist = new list<pvidscount>();
if (pvids != null)
{
foreach (var pvidscount in pvids)
{
var pvid = pvidscount.pvid;
var count = pvidscount.count;
var stock = _productservice.getproductvariantbyid(pvid).stockquantity;
if (stock - count < 0)
{
var pvidc=new pvidscount();
pvidc.pvid = pvid;
pvidc.count = stock;
resultlist.add(pvidc);
}
}
if (resultlist.count > 0)
{
return json(new { resultlist }); //json() ---mvc的json 方法会自动把list<t> ienumerable<t>转换为 json array<t>
}
else
{
return json("success");
}
}
return null;
}
public class pvidscount
{
public int pvid { set; get; }
public int count { set; get; }
}

---------------------------前台-------------------
复制代码 代码如下:

ajax
success: function (data) {
if (data == "success") {
}
} else {
$.each(data.resultlist, function (index, value) {
$("#item_pvid_" + value.pvid).html("this product's stock not enough.stock is " + value.count);
});
}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网