MVC后臺(tái)創(chuàng)建Json(List)前臺(tái)接受并循環(huán)讀取實(shí)例
更新時(shí)間:2013年06月09日 16:11:16 作者:
MVC后臺(tái)創(chuàng)建Json(List)同時(shí)前臺(tái)接受并循環(huán)讀取,具體實(shí)現(xiàn)代碼如下,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助
---------------------------后臺(tái)-------------------
[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 方法會(huì)自動(dòng)把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; }
}
---------------------------前臺(tái)-------------------
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 方法會(huì)自動(dòng)把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; }
}
---------------------------前臺(tái)-------------------
復(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動(dòng)態(tài)鏈接庫(kù)的方法
- VC創(chuàng)建進(jìn)程CreateProcess的方法
- VC實(shí)現(xiàn)動(dòng)態(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)用動(dòng)態(tài)鏈接庫(kù)實(shí)例詳解
- VC創(chuàng)建圓角dialog的實(shí)現(xiàn)方法
相關(guān)文章
ASP.Net防止刷新自動(dòng)觸發(fā)事件的解決方案
ASP.Net防止刷新自動(dòng)觸發(fā)事件的解決方案...2006-09-09
asp.net 編譯器錯(cuò)誤信息: CS0006: 未能找到元數(shù)據(jù)文件 該死的.NET
今天公司新上一臺(tái)志強(qiáng)虛擬主機(jī) 所有配置都好了 給客戶調(diào)整.net 出現(xiàn)了報(bào)錯(cuò)2009-06-06
Asp.net Core項(xiàng)目配置HTTPS支持
這篇文章介紹了Asp.net Core項(xiàng)目配置HTTPS支持的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
獲取轉(zhuǎn)向地址的URL的源文件(可自定義REFER)
獲取轉(zhuǎn)向地址的URL的源文件(可自定義REFER)...2006-09-09
visual Studio 2017創(chuàng)建簡(jiǎn)單控制臺(tái)程序
這篇文章主要為大家詳細(xì)介紹了visual Studio 2017創(chuàng)建簡(jiǎn)單控制臺(tái)程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11

