Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. [HttpGet]
  2. public JsonResult Eliminar(int id = 0)
  3. {
  4. try
  5. {
  6. Empleado empleado = empleados.Where(x => x.Id == id).FirstOrDefault();
  7. empleados.Remove(empleado);
  8. return Json(empleados, JsonRequestBehavior.AllowGet);
  9. }
  10. catch (Exception ex)
  11. {
  12.  
  13. return Json(false);
  14. }
  15. }
  16.  
  17. @section Scripts{
  18. <script type="text/javascript">
  19. $('.eliminar').click(function () {
  20. var idEliminar = $(this).attr("data-id");
  21. $("#btnEliminar").attr("data-eliminar", idEliminar);
  22. $("#myModal").modal();
  23. });
  24. $("#btnEliminar").click(function () {
  25. var idEliminar = $(this).attr("data-eliminar");
  26. $.ajax({
  27. url: '/Empleado/Eliminar',
  28. type: 'get',
  29. data: { id: idEliminar },
  30. dataType: 'json',
  31. success: function (result) {
  32. console.log("Exito");
  33. $("#" + idEliminar).remove();
  34. $("#myModal").modal('hide');
  35. },
  36. error: function () {
  37. console.log("fallo");
  38. }
  39. });
  40. });
  41. </script>
  42. }
  43.  
  44. public class Result
  45. {
  46. public string _result, _message, _URL;
  47. public object _body;
  48. public int? _cant;
  49. }
  50.  
  51. [HttpGet]
  52. public JsonResult Eliminar(int id = 0)
  53. {
  54. Result datos = null;
  55. try
  56. {
  57. Empleado empleado = empleados.Where(x => x.Id == id).FirstOrDefault();
  58. empleados.Remove(empleado);
  59. datos.message = "success";
  60. datos.result_ = "success";
  61. datos.cant = 1;
  62. }
  63. catch (Exception ex)
  64. {
  65. datos.message = ex.Message;
  66. datos.result_ = "error";
  67. }
  68. return Json(datos);
  69. }
  70.  
  71. @section Scripts{
  72. <script type="text/javascript">
  73. $('.eliminar').click(function () {
  74. var idEliminar = $(this).attr("data-id");
  75. $("#btnEliminar").attr("data-eliminar", idEliminar);
  76. $("#myModal").modal();
  77. });
  78. $("#btnEliminar").click(function () {
  79. var idEliminar = $(this).attr("data-eliminar");
  80. $.ajax({
  81. url: '/Empleado/Eliminar',
  82. type: 'get',
  83. data: { id: idEliminar },
  84. dataType: 'json',
  85. contentType: "application/json;charset=utf-8",
  86. success: function (result) {
  87. if(result.result_ == 'success'){
  88. console.log("Exito");
  89. $("#" + idEliminar).remove();
  90. $("#myModal").modal('hide');
  91. }else{
  92. console.log("error", result.message);
  93. }
  94. },
  95. error: function (err) {
  96. console.log(err);
  97. }
  98. });
  99. });
  100. </script>
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement