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

又是一场大雪过后,天空像海一样蔚蓝,甚至比海更加晶莹剔透。千峰万岭,极目望去,尽是白色,闪耀着一片连接不断的银光。山顶积雪未融,如白银宫网。
---------------------------后台-------------------
 
[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);
});
}
}

本文MVC后台创建Json(List)前台接受并循环读取实例到此结束。不要因为结束而哭泣,微笑吧,为你的曾经拥有。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
详解MVC中为DropDownListFor设置选中项的方法

浅谈MVC框架的优点(翻译)

写简单的mvc框架实例讲解

mvc实现图片验证码功能

MVC框架是什么 这里为你解答