MVC Background creates Json of List foreground to receive and loop read instances

  • 2020-06-15 08:00:59
  • OfStack

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the background -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
[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 the JSON  The method will automatically put List<T> IEnumerable<T> convert  Json Array<T> 
} 
else 
{ 
return Json("success"); 
} 
} 
return null; 
} 
public class pvIdsCount 
{ 
public int pvId { set; get; } 
public int count { set; get; } 
} 

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- at the front desk -- -- -- -- -- -- -- -- -- --
 
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); 
}); 
} 
} 

Related articles: