2011-01-13 36 views
83

Estoy tratando de implementar un simple ActionLink que borrará los registros utilizando ASP.NET MVC. Esto es lo que tengo hasta ahora:Eliminar ActionLink con el cuadro de diálogo de confirmación

<%= Html.ActionLink("Delete", 
        "Delete", 
        new { id = item.storyId, 
          onclick = "return confirm('Are you sure?');" 
         })%> 

Sin embargo, no muestra el cuadro de confirmación. Claramente me falta algo o he construido incorrectamente el enlace. ¿Alguien puede ayudar?

Respuesta

176

No se debe confundir con routeValueshtmlAttributes . Es posible que desee this overload:

<%= Html.ActionLink(
    "Delete", 
    "Delete", 
    new { id = item.storyId }, 
    new { onclick = "return confirm('Are you sure you wish to delete this article?');" }) 
%> 
+0

Eso es perfecto. Gracias. – Cameron

+3

Impresionante señor .... Estaba buscando mucho y pensando dónde está Darin ..... :) –

+14

¡Evite eliminar registros al solicitar GET! http: // stackoverflow.com/questions/786070/why-should-you-delete-using-an-http-post-or-delete-rather-get-get – user1068352

13

esos son rutas que está pasando en

<%= Html.ActionLink("Delete", "Delete", 
    new { id = item.storyId }, 
    new { onclick = "return confirm('Are you sure you wish to delete this article?');" })  %> 

El método sobrecargado que estás buscando es éste:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    Object routeValues, 
    Object htmlAttributes 
) 

http://msdn.microsoft.com/en-us/library/dd492124.aspx

13
<%= Html.ActionLink("Delete", "Delete", 
    new { id = item.storyId }, 
    new { onclick = "return confirm('Are you sure you wish to delete this article?');" })  %> 

El código anterior sólo funciona para Html.ActionLink.

Para

Ajax.ActionLink

utilizar el siguiente código:

<%= Ajax.ActionLink(" ", "deleteMeeting", new { id = Model.eventID, subid = subItem.ID, fordate = forDate, forslot = forslot }, new AjaxOptions 
              { 
               Confirm = "Are you sure you wish to delete?", 
               UpdateTargetId = "Appointments", 
               HttpMethod = "Get", 
               InsertionMode = InsertionMode.Replace, 
               LoadingElementId = "div_loading" 
              }, new { @class = "DeleteApointmentsforevent" })%> 

La opción 'Confirmar' especifica Javascript cuadro de confirmación.

-2

También puede probar esto por Html.ActionLink DeleteId

+2

¿Puedes explicar esta respuesta un poco? Tal vez proporcione un fragmento de código que demuestre su sugerencia o describa en qué parte del código del OP debe ir esto. – skrrgwasme

1

También puede personalizar el pasando el elemento de borrado junto con el mensaje. En mi caso usando MVC y la maquinilla de afeitar, por lo que podría hacer esto:

@Html.ActionLink("Delete", 
    "DeleteTag", new { id = t.IDTag }, 
    new { onclick = "return confirm('Do you really want to delete the tag " + @t.Tag + "?')" }) 
1

Prueba esto:

<button> @Html.ActionLink(" ", "DeletePhoto", "PhotoAndVideo", new { id = item.Id }, new { @class = "modal-link1", @OnClick = "return confirm('Are you sure you to delete this Record?');" })</button> 
1

Con la imagen y la confirmación de borrado, que funciona en Mozilla Firefox

<button> @Html.ActionLink(" ", "action", "controller", new { id = item.Id }, new { @class = "modal-link1", @OnClick = "return confirm('Are you sure you to delete this Record?');" })</button> 
<style> 
a.modal-link{ background: URL(../../../../Content/Images/Delete.png) no-repeat center; 
      display: block; 
      height: 15px; 
      width: 15px; 

     } 
</style> 
0

Al usar la cuadrícula web you can found it here, los enlaces de acción podrían ser similares a los siguientes.

enter image description here

grid.Column(header: "Action", format: (item) => new HtmlString(
        Html.ActionLink(" ", "Details", new { Id = item.Id }, new { @class = "glyphicon glyphicon-info-sign" }).ToString() + " | " + 
        Html.ActionLink(" ", "Edit", new { Id = item.Id }, new { @class = "glyphicon glyphicon-edit" }).ToString() + " | " + 
        Html.ActionLink(" ", "Delete", new { Id = item.Id }, new { onclick = "return confirm('Are you sure you wish to delete this property?');", @class = "glyphicon glyphicon-trash" }).ToString() 
       ) 
-1

Cualquier evento de clic antes de actualizar/editar/borrar registros de mensajes de alerta al usuario de la caja y si "Ok", inicio de la acción más "cancelar" se mantienen sin cambios. Para este código, no es necesario corregir el código del script java por separado. funciona para mí

<a asp-action="Delete" asp-route-ID="@Item.ArtistID" onclick = "return confirm('Are you sure you wish to remove this Artist?');">Delete</a>

Cuestiones relacionadas