欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-29 20:59:46
---------------------------后台------------------- 复制代码 代码如下: [httppost] public jsonresu...
---------------------------后台-------------------
复制代码 代码如下:

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