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

标签 c# asp.net-mvc linq error-handling

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

        [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/

相关文章:

c# - 无法捕获 sleep /暂停消息 (winXP)

jquery - 使用 jQuery 日期选择器从 ASP MVC @Html.TextBoxFor 控件中删除时间

c# - 查找数组中最大值的出现

c# - 将 DateTime 格式化为英文速记月份

asp.net-mvc - 文件名作为 MVC Controller 的最后一个参数

c# - System.Linq 和 System.Data.Linq 有什么区别?

c# - 将字符串转换为结构的快速方法

c# - 访问数组时的速度差异

c# - 缺少 System.Windows.Shell 引用

c# - 找到正确的线程来调用方法而不会失败