2011-01-13 12 views
83

Ich versuche, eine einfache ActionLink zu implementieren, die Datensätze mit ASP.NET MVC löschen wird. Dies ist, was ich bisher habe:Delete ActionLink mit bestätigen Dialog

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

Allerdings zeigt es nicht die Bestätigungsbox. Offensichtlich fehlt mir etwas oder ich habe den Link falsch aufgebaut. Kann jemand helfen?

Antwort

176

Verwechseln Sie nicht routeValues mit htmlAttributes . Sie wollen wahrscheinlich this overload:

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

Das ist perfekt. Vielen Dank. – Cameron

+3

Awesome sir .... Ich suchte viel und denke, wo ist Darin ..... :) –

+14

Vermeiden Sie Datensätze auf GET-Anfrage zu löschen! http: // Stapelüberlauf.com/questions/786070/why-should-you-delete-verwenden-ein-http-post-or-delete-lieber als – user1068352

13

diejenigen sind Routen, die Sie vorbei sind in

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

Die überladene Methode, die Sie suchen ist diese:

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

Der obige Code funktioniert nur für Html.ActionLink.

Für

Ajax.ActionLink

verwenden Sie folgenden Code:

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

Die 'Bestätigen' Option gibt Javascript bestätigen Feld.

-2

Sie auch diese für Html.ActionLink versuchen DeleteId

+2

Können Sie diese Antwort ein wenig erklären? Vielleicht ein Code-Snippet bereitstellen, das deinen Vorschlag zeigt oder beschreiben, wo im Code des OPs das gehen soll? – skrrgwasme

1

Sie können auch besonders anfertigen, indem das Löschen des Elements zusammen mit der Nachricht übergeben. In meinem Fall MVC und Razor verwenden, so könnte ich dies tun:

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

Versuchen:

<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

Mit Bild und Bestätigung zu löschen, die firefox auf mozilla arbeitet

<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

Mit webgrid you can found it here könnten die Aktionslinks wie folgt aussehen.

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

Alle vor Ereignis klicken für Update/bearbeiten/der Benutzer Datensätze Meldungsfeld Benachrichtigung löschen und wenn „Ok“ für die Aktion else „Abbrechen“ bleiben unverändert fort. Für diesen Code muss Java Script Code nicht richtig getrennt werden. es funktioniert für mich

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

Verwandte Themen