2011-01-13 27 views
83

Sto cercando di implementare un semplice ActionLink che eliminerà i record utilizzando ASP.NET MVC. Questo è quello che ho finora:Elimina actionLink con conferma

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

Tuttavia, non mostra la casella di conferma. Chiaramente mi manca qualcosa o non ho costruito correttamente il link. Qualcuno può aiutare?

risposta

176

Non confondere routeValues con htmlAttributes . Probabilmente si desidera this overload:

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

Questo è perfetto. Grazie. – Cameron

+3

Fantastico signore .... Stavo cercando molto e sto pensando a dove si trova Darin ..... :) –

+14

Evita di eliminare i record su richiesta GET! http: // StackOverflow.it/questions/786070/why-should-you-delete-using-an-http-post-or-delete-piuttosto-than-get – user1068352

13

quelli sono percorsi si sta passando in

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

Il metodo di overload che stai cercando è questo:

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?');" })  %> 

Il codice precedente funziona solo per Html.ActionLink.

Per

Ajax.ActionLink

utilizzare il seguente codice:

<%= 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" })%> 

L'opzione 'Conferma' specifica JavaScript di dialogo Conferma.

-2

Si può anche provare questo per Html.ActionLink DeleteId

+2

Puoi spiegare questa risposta un po '? Forse fornire uno snippet di codice che mostri il tuo suggerimento o descriva dove dovrebbe andare nel codice dell'OP? – skrrgwasme

1

È inoltre possibile personalizzare l'passando la voce Elimina insieme al messaggio. Nel mio caso usando MVC e Razor, così ho potuto fare questo:

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

Prova questo:

<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 l'immagine e la conferma sulla cancellazione, che funziona su 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

Utilizzando la griglia Web you can found it here, i collegamenti di azione potrebbero essere come i seguenti.

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

Qualsiasi evento click prima per l'aggiornamento/modificare/cancellare i record finestra di messaggio avvisa l'utente e se "Ok" procedere per l'azione altro "cancel" rimangono invariati. Per questo codice non è necessario il diritto di separare il codice di script java. funziona per me

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