【发布时间】:2015-11-12 10:52:13
【问题描述】:
我真的不知道怎么问这个。我的控制器中有一组异步操作,其中一个应该通过 foreach 语句填充列表。奇怪的是,假设我有 10 行代码,它运行第 1 行,然后跳到第 4 行,然后跳到第 10 行,然后又跳到第 1 行,在某一时刻,列表包含它应该包含的所有项目,但随后又跳到第一个我声明该列表的行,然后它与空列表一起存在。谁能解释为什么会发生这种情况以及如何解决?
这是有问题的操作
[HttpGet]
public async Task<ActionResult> GetBuyingListItems(string buyingListId, string vendorId, string orderId)
{
var buyingListService = new BuyingListService(_buyingListRepository);
ItemService itemService = new ItemService(_itemRepository);
var vendorService = new VendorService(_vendorRepository);
om.BuyingList = await buyingListService.GetOne(buyingListId);
om.BuyingListItems = await buyingListService.GetBuyingListItems(buyingListId);
om.Vendor = await vendorService.GetOne(vendorId);
om.ItemsList = new List<OrderItemsDTO>();
foreach (var buyingListItem in om.BuyingListItems)
{
var item = await itemService.GetOne(buyingListItem.ItemId);
var orderItem = new OrderItemsDTO();
orderItem.OrderId = orderId;
orderItem.ItemID = item.Id;
orderItem.Qty = buyingListItem.Qty;
orderItem.TotalPrice = item.UnitPrice * buyingListItem.Qty;
orderItem.Currency = om.Vendor.Currency;
orderItem.VendorId = om.Vendor.Id;
om.ItemsList.Add(orderItem);
}
return RedirectToAction("ItemListWithBuyingList", new { vendorId = vendorId, orderId = orderId, ItemsList = om.ItemsList });
}
om -> 这是模型,我将它用于整个控制器。
附:我不知道如何制定这个问题以寻找答案,如果已经回答,我很抱歉。
p.p.s.这不是唯一像这样运行的方法,但直到现在我从来没有遇到过这个问题。我一直认为这是因为它是异步运行的。
p.p.p.s 这可能会有所帮助,如果我从浏览器调试,在网络部分,我的 ajax 调用被多次进行(例如:第一次 - 3 次,然后是 6 次,然后是 12 次,等等...... )。我在我的 ajax 调用中返回 false,并且我的代码中没有 href="#"。
谢谢。
【问题讨论】:
-
除非您在问题中包含一些代码,否则我认为您不会得到答案
-
刚刚添加了代码,谢谢
标签: c# asp.net-mvc asynchronous