MVC后臺創(chuàng)建Json(List)前臺接受并循環(huán)讀取實例
更新時間:2013年06月09日 16:11:16 作者:
MVC后臺創(chuàng)建Json(List)同時前臺接受并循環(huán)讀取,具體實現(xiàn)代碼如下,感興趣的朋友可以參考下哈,希望對大家有所幫助
---------------------------后臺-------------------
[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>轉(zhuǎn)換為 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);
});
}
}
復(fù)制代碼 代碼如下:
[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>轉(zhuǎn)換為 Json Array<T>
}
else
{
return Json("success");
}
}
return null;
}
public class pvIdsCount
{
public int pvId { set; get; }
public int count { set; get; }
}
---------------------------前臺-------------------
復(fù)制代碼 代碼如下:
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);
});
}
}
您可能感興趣的文章:
- VC創(chuàng)建DLL動態(tài)鏈接庫的方法
- VC創(chuàng)建進(jìn)程CreateProcess的方法
- VC實現(xiàn)動態(tài)菜單的創(chuàng)建方法
- VC++創(chuàng)建msi文件的方法
- MVC 5 第一章 創(chuàng)建MVC 5 web應(yīng)用程序
- c#創(chuàng)建vc可調(diào)用的com組件方法分享
- 解析VC中創(chuàng)建DLL,導(dǎo)出全局變量,函數(shù)和類的深入分析
- VC6.0如何創(chuàng)建以及調(diào)用動態(tài)鏈接庫實例詳解
- VC創(chuàng)建圓角dialog的實現(xiàn)方法
相關(guān)文章
asp.net 編譯器錯誤信息: CS0006: 未能找到元數(shù)據(jù)文件 該死的.NET
今天公司新上一臺志強虛擬主機 所有配置都好了 給客戶調(diào)整.net 出現(xiàn)了報錯2009-06-06
獲取轉(zhuǎn)向地址的URL的源文件(可自定義REFER)
獲取轉(zhuǎn)向地址的URL的源文件(可自定義REFER)...2006-09-09
visual Studio 2017創(chuàng)建簡單控制臺程序
這篇文章主要為大家詳細(xì)介紹了visual Studio 2017創(chuàng)建簡單控制臺程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11

