Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
How to use Form into foreach loop in Razor view

I used sweet alert dialog to confirm the deletion. When I enter the model id value in the form, the correct value is not sent and one less number is sent.

The correct id value is sent without using the Form.

Please tell me where the problem is and how I can solve it.

$(".ajax_delete1").on("click", function validateForm(event) {
  event.preventDefault(); // prevent form submit
  var form = $('#ppp'); // storing the form
  swal({
      title: "آیا از پاک کردن این ایتم مطمئن هستید",
      text: "این ایتم اگر حذف شود دیگر قابل بازگردانی نیست!",
      icon: "warning",
      showCancelButton: true,
      dangerMode: true,
      confirmButtonColor: '#DD6B55',
      confirmButtonText: "حذف",
      cancelButtonText: "انصراف",

    })
    .then((willDelete) => {
      if (willDelete) {

        form.submit();
      } else {
        swal("فایل شما پاک نشد!");
      }
    });
});
@foreach (var item in Model) {

<tr>
  <td>@item.projectName</td>
  <td>@item.projectMagName</td>
  <td style="width:250px">

    <div class="text-center row d-flex justify-content-between">
      <div class="mt-1">
        <a class="fa fa-edit" style="font-size:28px;color:darkblue" asp-controller="Home" asp-action="EditProject" asp-route-id="@item.projectID"></a>
      </div>

      <div style="margin-top:3px">


        <form asp-controller="Home" asp-action="RemoveProject" asp-route-id="@item.projectID" method="post" id="pop">

          <a class="fa fa-trash-o ajax_delete1" style="font-size: 28px; color: red; cursor: pointer;"></a>


        </form>
      </div>

      <div style="margin-top:1px">
        <a class="btn btn-sm btn-outline-primary" asp-controller="Home" asp-action="ProjectDetails" asp-route-id="@item.projectID">جزئیات</a>

        <a class="btn btn-sm btn-outline-secondary" asp-controller="Home" asp-action="PayShow" asp-route-id="@item.projectID">دسترسی</a>
      </div>

    </div>


  </td>


</tr>

}

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

According to your description, I found you use $('#pop');, if you use query codes, it will always use the first form.

I suggest you could try to use var form = event.target.closest("form");, this will find the A tag's closet form element.

More details, you could refer to below codes:

$(".ajax_delete1").on("click", function validateForm(event) {
  event.preventDefault(); // prevent form submit
  var form = event.target.closest("form"); // storing the form
  swal({
      title: "آیا از پاک کردن این ایتم مطمئن هستید",
      text: "این ایتم اگر حذف شود دیگر قابل بازگردانی نیست!",
      icon: "warning",
      showCancelButton: true,
      dangerMode: true,
      confirmButtonColor: '#DD6B55',
      confirmButtonText: "حذف",
      cancelButtonText: "انصراف",

    })
    .then((willDelete) => {
      if (willDelete) {

        form.submit();
      } else {
        swal("فایل شما پاک نشد!");
      }
    });
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda