Advertisement
Guest User

Untitled

a guest
Nov 7th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. //пик 1
  2. <form method="delete" asp-route="ProductCatalogSingle">
  3. <input hidden type="number" name="Id" value="@Model.Id" />
  4. <input type="submit" class="btn btn-dark" value="Delete" />
  5. </form>
  6.  
  7. //пик 2
  8.  
  9. // Controller have [Route("product-catalog")] attribute
  10.  
  11. [HttpDelete("product", Name = "ProductCatalogSingle")]
  12. public async Task<IActionResult> DeleteSingleItem(int id)
  13. {
  14. // business-logic part responsible for data managment
  15. await logic.RemoveDataModelAsync(new ProductCatalog { Id = id });
  16. return RedirectToRoute("ProductCatalogAll");
  17. }
  18.  
  19. // other action with same route, but different method
  20. [HttpGet("product", Name = "ProductCatalogSingle")]
  21. public async Task<IActionResult> SingleItem(int id)
  22. {...}
  23.  
  24. // пик 3
  25. // "/product-catalog/product" and "@Url.Link("ProductCatalogSingle", null)" вместо url
  26. @section scripts{
  27. <script>
  28. document.forms[0].onsubmit = () => {
  29. let formData = new FormData(document.forms[0]);
  30. fetch(url, {
  31. method: "DELETE",
  32. body: new URLSearchParams(formData)
  33. })
  34. .then(() => {
  35. alert('Deleted using Fetch');
  36. });
  37. return true;
  38. };
  39. </script>
  40.  
  41. // пик 4
  42. @section scripts{
  43. <script>
  44. $(function () {
  45. $('#submit').on('click', function (evt) {
  46. evt.preventDefault();
  47. $.ajax({
  48. type: "DELETE",
  49. url: '/product-catalog/product?id=4001'
  50. });
  51. });
  52. });
  53. </script>
  54.  
  55. // Ажакс анобструксив
  56. @section scripts{
  57. <script src="~/lib/jquery-ajax-unobtrusive/jquery.unobtrusive-ajax.js"></script>
  58. }
  59.  
  60. <form method="delete" asp-route="ProductCatalogSingle" data-ajax="true" data-ajax-method="delete">
  61. <input hidden type="number" name="productId" value="@item.ProductId" />
  62. <input type="submit" class="btn btn-dark" value="Delete" />
  63. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement