gpt4 book ai didi

c# - 从 C# 中的 void 方法返回错误消息

转载 作者:行者123 更新时间:2023-12-03 08:19:18 25 4
gpt4 key购买 nike

如果条件无效,我目前正在尝试让我的方法返回错误消息。但我不确定如何以无效的方法解决这个问题。
我有一个看起来像这样的方法

        [HttpPost]
[ValidateAntiForgeryToken]
[Audit]
public void AddUnits(int so_id, int site_id, int[] addItem_id, int[] addItem_qty, int[] addItem_disc)
{
// Loop however many times is necessary to iterate through the largest array
for (int i = 0; i < Math.Max(Math.Max(addItem_id.Length, addComp_id.Length), addPart_id.Length); i++)
{
foreach (SODetails sod in db.SalesOrders.Find(so_id).SalesDetails)
{
if (i < addItem_id.Length && addItem_qty[i] != 0 && sod.ItemID == addItem_id[i] && addItem_id[i] != 365 && addItem_id[i] != 410)
{

sod.item_qty += addItem_qty[i];
sod.item_discount = addItem_disc[i];
addItem_id[i] = 0;
addItem_qty[i] = 0;
addItem_disc[i] = 0;
}
}
db.SaveChanges();

if(i < addItem_qty.Length && addItem_qty[i] != 0)
{
SODetails sODetails = new SODetails
{
SalesOrderID = so_id,
SiteID = site_id
};

// Only add a unit to the SODetails object if it's not null and has an id and quanitity specified
if(i < addItem_id.Length && addItem_id[i] != 0 && addItem_qty[i] != 0)
{
sODetails.ItemID = addItem_id[i];
sODetails.item_qty = addItem_qty[i];
sODetails.item_discount = addItem_disc[i];
}

if (sODetails.SiteID == 0)
sODetails.SiteID = null;


SalesOrder SO = db.SalesOrders.Find(sODetails.SalesOrderID);
SODetails salesOrderDetails = db.SODetails.Add(sODetails);
salesOrderDetails.SalesOrder = SO;

Item SO_Item = db.Items.Find(sODetails.ItemID);
Component SO_Component = db.Components.Find(sODetails.ComponentID);
Part SO_Part = db.Parts.Find(sODetails.PartID);

if (SO_Item != null)
{
if (SO.OrderType == SOType.OffSiteInventory && sODetails.InventorySite == "Main Inventory" && SO_Item.On_Hand < salesOrderDetails.item_qty)
{

ModelState.AddModelError(string.Empty, "Not enough stock in inventory");
//TempData["SalesOrderMessage"] = SO_Item.SalesOrderMessage;

}

sODetails.item_qty = sODetails.item_qty == null ? 0 : sODetails.item_qty;
int qtyOrdered = sODetails.item_qty == null ? 0 : (int)sODetails.item_qty;
salesOrderDetails.dynamicItem_qty = qtyOrdered;

if (SO_Item.SalesOrderMessage != null)
TempData["SalesOrderMessage"] = SO_Item.SalesOrderMessage;
}

db.SODetails.Add(sODetails);

}
}

db.SaveChanges();
}

这是我正在进行验证检查的代码部分
 if (SO.OrderType == SOType.OffSiteInventory && sODetails.InventorySite == "Main Inventory" && SO_Item.On_Hand < salesOrderDetails.item_qty)
{

ModelState.AddModelError(string.Empty, "Not enough stock in inventory");
//TempData["SalesOrderMessage"] = SO_Item.SalesOrderMessage;

}
如果条件有效,我希望它返回屏幕并显示错误消息。但是使用 Void 的方法,我不知道如何使它做到这一点,或者我什至不知道它是否可能。

最佳答案

您可以创建一个可以在 void 函数中抛出的特定异常类。然后,您在调用函数中处理此异常。

class NotEnoughStockException : Exception {}

然后在你的方法中:
If no stock ...
throw new NotEnoughStockException();

在调用方法中
try {
call the stock method
} catch NotEnoughStockException {
whatever you want to do
}

创建一个将调用您的股票函数的包装函数。您确实尝试在该新函数中捕获并返回错误消息。您的 Ajax 应该调用新函数。

关于c# - 从 C# 中的 void 方法返回错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61235284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com