使用PUT更新可以添加条目,但不能删除条目

问题描述

[HttpPut("edit/{id}")]
        //[Route("api/{controller}/edit/{id}")]
        public async Task<IActionResult> PutOrder(int id,Order order)
        {
            if (id != order.OrderID)
            {
                return BadRequest();
            }

            try
            {

                _context.Orders.Update(order);
                await _context.SaveChangesAsync();
            }
            catch (dbupdateConcurrencyException)
            {
                if (!dispatchRequestExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

按照标题,为什么会这样呢?因此,在选定订单的表中有一个订单项列表,我可以在其中删除添加更多条目,然后单击“提交”进行更改。目前,我可以添加但不能删除提交后的条目。我如何也可以删除条目?

我已经可以下订单并删除订单。但是,我现在正在创建一种功能来编辑订单并更改其中的字段,例如订单项。如果我编辑订单并添加更多订单项,则可以使用,但是当我删除最初存在的某些订单项时,则无法使用。编辑所做的更改是在单击“提交”按钮时进行的,而不是在单击“删除”按钮以从表中删除订单项或添加其他项时进行的。我该怎么做?请赐教。

解决方法

此功能中没有任何删除命令。如果要删除实体,请使用它。

_context.YourTable.Remove(YourEntity);