Delete ActionLink with confirm dialog
I'm trying to implement a simple ActionLink
that will delete records using ASP.NET MVC. This is what I have so far:
<%= Html.ActionLink("Delete",
"Delete",
new { id = item.storyId,
onclick = "return confirm('Are you sure?');"
})%>
However, it do开发者_开发问答esn't show the confirm box. Clearly I'm missing something or I have incorrectly built the link. Can anyone help?
Don't confuse routeValues
with htmlAttributes
. You probably want this overload:
<%= Html.ActionLink(
"Delete",
"Delete",
new { id = item.storyId },
new { onclick = "return confirm('Are you sure you wish to delete this article?');" })
%>
those are routes you're passing in
<%= Html.ActionLink("Delete", "Delete",
new { id = item.storyId },
new { onclick = "return confirm('Are you sure you wish to delete this article?');" }) %>
The overloaded method you're looking for is this one:
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
Object routeValues,
Object htmlAttributes
)
http://msdn.microsoft.com/en-us/library/dd492124.aspx
<%= Html.ActionLink("Delete", "Delete",
new { id = item.storyId },
new { onclick = "return confirm('Are you sure you wish to delete this article?');" }) %>
The above code only works for Html.ActionLink.
For
Ajax.ActionLink
use the following 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" })%>
The 'Confirm' option specifies javascript confirm box.
You can also customize the by passing the delete item along with the message. In my case using MVC and Razor, so I could do this:
@Html.ActionLink("Delete",
"DeleteTag", new { id = t.IDTag },
new { onclick = "return confirm('Do you really want to delete the tag " + @t.Tag + "?')" })
Try this :
<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>
Using webgrid you can found it here, the action links could look like the following.
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()
)
With image and confirmation on delete, which works on 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>
I wanted the same thing; a delete button on my Details view. I eventually realised I needed to post from that view:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.Id)
@Html.ActionLink("Edit", "Edit", new { id = Model.Id }, new { @class = "btn btn-primary", @style="margin-right:30px" })
<input type="submit" value="Delete" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this record?');" />
}
And, in the Controller:
// this action deletes record - called from the Delete button on Details view
[HttpPost]
public ActionResult Details(MainPlus mainPlus)
{
if (mainPlus != null)
{
try
{
using (IDbConnection db = new SqlConnection(PCALConn))
{
var result = db.Execute("DELETE PCAL.Main WHERE Id = @Id", new { Id = mainPlus.Id });
}
return RedirectToAction("Calls");
} etc
@Html.Raw(HttpUtility.HtmlDecode(@Html.ActionLink(" ", "Action", "Controller", new { id = model.id }, new { @class = "glyphicon glyphicon-trash", @OnClick = "return confirm('Are you sure you to delete this Record?');" }).ToHtmlString()))
Any click event before for update /edit/delete records message box alerts the user and if "Ok" proceed for the action else "cancel" remain unchanged. For this code no need to right separate java script code. it works for me
<a asp-action="Delete" asp-route-ID="@Item.ArtistID" onclick = "return confirm('Are you sure you wish to remove this Artist?');">Delete</a>
You can also try this for Html.ActionLink DeleteId
精彩评论