2011-01-13 12 views
83

Próbuję zaimplementować prosty ActionLink, który usunie rekordy przy użyciu środowiska ASP.NET MVC. Oto, co mam do tej pory:Usuwanie połączenia ActionLink z potwierdzeniem dialogowym

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

Jednak nie pokazuje pola potwierdzenia. Najwyraźniej czegoś brakuje lub nieprawidłowo zbudowałem link. Czy ktoś może pomóc?

Odpowiedz

176

Nie należy mylić routeValues z htmlAttributes . Prawdopodobnie chcesz this overload:

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

To jest idealne. Dziękuję Ci. – Cameron

+3

Awesome sir .... Dużo szukałem i zastanawiałem się, gdzie jest Darin ..... :) –

+14

Unikaj usuwania zapisów na żądanie GET! http: // stackoverflow.com/questions/786070/why-should-you-delete-using-an-http-post-or-delete-a-than-than-get – user1068352

13

są to trasy jesteś przejazdem w

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

przeciążonej metody szukasz jest to jedno:

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

Powyższy kod działa tylko dla Html.ActionLink.

Dla

Ajax.ActionLink

użyć następującego kodu:

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

Opcja 'Potwierdź' określa javascript pole potwierdzić.

-2

Można też spróbować to dla Html.ActionLink DeleteId

+2

Czy możesz wyjaśnić tę odpowiedź trochę? Może dostarczyć fragment kodu, który demonstruje twoją sugestię lub opisać, gdzie w kodzie OP powinno to iść? – skrrgwasme

1

Można również dostosować przekazując pozycję usuwania wraz z komunikatem. W moim przypadku przy użyciu MVC i wyjątkowo, więc mogę to zrobić:

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

Spróbuj tego:

<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

z obrazem i potwierdzenia na usuwanie, który działa na 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

Korzystanie z webgrid you can found it here, łącza akcji mogą wyglądać następująco.

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

Każde zdarzenie click zanim do aktualizacji/edytować/usuwać rekordy powiadomienia okno komunikatu i jeśli użytkownik „Ok” przystąpić do działania innego „anuluj” pozostają bez zmian. Dla tego kodu nie ma potrzeby poprawiania osobnego kodu skryptu java. działa dla mnie

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

Powiązane problemy