zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

2023-06-13 09:15:01 时间
---------------------------后台-------------------
复制代码代码如下:

[HttpPost]
publicJsonResultCheckStock(IEnumerable<pvIdsCount>pvIds)
{
varresultList=newList<pvIdsCount>();
if(pvIds!=null)
{
foreach(varpvIdsCountinpvIds)
{
varpvId=pvIdsCount.pvId;
varcount=pvIdsCount.count;
varstock=_productService.GetProductVariantById(pvId).StockQuantity;
if(stock-count<0)
{
varpvIdC=newpvIdsCount();
pvIdC.pvId=pvId;
pvIdC.count=stock;
resultList.Add(pvIdC);
}
}
if(resultList.Count>0)
{
returnJson(new{resultList});//Json()---MVC的JSON方法会自动把List<T>IEnumerable<T>转换为JsonArray<T>
}
else
{
returnJson("success");
}
}
returnnull;
}
publicclasspvIdsCount
{
publicintpvId{set;get;}
publicintcount{set;get;}
}

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

AJAX
success:function(data){
if(data=="success"){
}
}else{
$.each(data.resultList,function(index,value){
$("#Item_PVId_"+value.pvId).html("ThisProduct"sStockNotEnough.Stockis"+value.count);
});
}
}