Hi I am using an edit button inside a gridview. I want a confirmation button before calling to the action?
grid.Column("","",format:@<text>@if(!item.IsBookPublished)
{
<text> <a href='@Url.Action("EditBookByID","Books", new {BookID = @item.BookDetailsID, CreatedBy = @item.UserID , onclick = "return confirm('Are you sure you want to Edit?')" })'>Edit</a></text>
}
</text>
However the onclick property is not evaluating, instead it is passing as a parameter. How can I achieve confirmation?
You've placed it at the wrong place. Right now you've passed it as parameter to the Url.Action helper, whereas it should be a separate attribute, the same way you defined the href attribute:
<a href="@Url.Action("EditBookByID", "Books", new { bookID = item.BookDetailsID, CreatedBy = item.UserID })" onclick="return confirm('Are you sure you want to Edit?')">Edit</a>
By the way you should consider using helpers for that:
grid.Column("", "", format:
@<text>
@if(!item.IsBookPublished)
{
Html.ActionLink(
"Edit",
"EditBookByID",
"Books",
new { bookID = @item.BookDetailsID },
new { onclick = "return confirm('Are you sure you want to Edit?')" }
)
}
</text>
)
By putting the 'onclick' inside the Url.Action helper, you're telling it to translate it as a URL parameter.
What you want to do instead is put the onclick outside the helper like this:
<a href='@Url.Action("EditBookByID","Books", new {BookID = @item.BookDetailsID, CreatedBy = @item.UserID })' onclick = "return confirm('Are you sure you want to Edit?')">
Edit
<a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With