title: How do I delete from the database in Asp.Net Core via Ajax?
body: <p>This script is supposed to send a <code>ProductId</code> to the home controller's <code>Delete</code>-method, and the controller should make the appropriate <code>Remove</code>-operation:</p>
  
  <pre><code>$('[name="DeleteItem"]').click(function (e) {
      $.ajax({
          type: "DELETE",
          url: "@Url.Action('Delete','Home')",
          data: { id: $('DeleteItem#data-id').val() },
          success: function () {
              alert("success!");
              window.location.replace("@Url.Action('Index', 'Home')");
          },
          error: function (data) {
              alert("Error: " + data.id);
          }
      });
  });
  </code></pre>
  
  <p>This is the form:</p>
  
  <pre><code>&lt;form asp-action="Update"&gt;
      @foreach (var item in Model.ShoppingCartItems)
      {
          @item.ProductTitle
          &lt;input asp-for="@item.Quantity" /&gt;
          &lt;button name="DeleteItem" data-id="@item.ProductId"&gt;DELETE&lt;/button&gt;
      }
      &lt;button type="submit"&gt;Update quantity&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>This is the controller's <code>Delete</code>-method (I don't have the <code>ShoppingCartId</code>, so I'm getting it based on <code>SessionId</code>, which is stored in the <code>ShoppingCarts</code>-table):</p>
  
  <pre><code>[HttpDelete]
  //[ValidateAntiForgeryToken] // &lt;-- Do I need this in this case?
  public async Task&lt;IActionResult&gt; Delete(
          [Bind("ShoppingCartItemProductId")]
          ViewModelAddToCart model)
  {
      // Initialize session to enable SessionId
      HttpContext.Session.SetString("_Name", "MyStore");
      string SessionId = HttpContext.Session.Id;
  
      var ShoppingCart = new ShoppingCart()
      {
          SessionId = SessionId
      };
  
      var ShoppingCartItem = new ShoppingCartItem()
      {
          ProductId = model.ShoppingCartItemProductId,
      };
  
      if (ModelState.IsValid)
      {
          // Find ShoppingCart containing current SessionId.
          var cartInfo =
              (from Cart in _context.ShoppingCarts
               where Cart.SessionId == SessionId
               select new { TempId = Cart.Id })
                  .SingleOrDefault();
          if (cartInfo != null)
          {
              ShoppingCartItem.ShoppingCartId = cartInfo.TempId;
          }
  
          // Find ShoppingCartItem containing current ProductId:
          var cartItemInfo =
              (from CartItem in _context.ShoppingCartItems
               where (CartItem.ShoppingCartId == ShoppingCartItem.ShoppingCartId &amp;&amp;
                      CartItem.ProductId == model.ShoppingCartItemProductId)
                   select new { TempId = CartItem.Id })
                  .FirstOrDefault();
          if (cartItemInfo != null)
          {
              // Delete ShoppingCartItem
              ShoppingCartItem.Id = cartItemInfo.TempId;
              _context.ShoppingCartItems.Remove(ShoppingCartItem);
          }
          await _context.SaveChangesAsync();
          return RedirectToAction("Index", "Home");
      }
      else
      {
          return View("Index", "Home");
      }
  }
  </code></pre>
  
  <p><strong>Edit</strong> I have made some changes to my code, and now I receive "Error: undefined" in an alert. That is because the <code>error:</code> in the ajax is triggered, and the <code>data</code>-object is not defined. Why is that? And a second question is what is the controller supposed to return? As I understand, not a <code>RedirectToAction</code>.</p>