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?
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?
Share Improve this question edited Jul 9, 2013 at 13:54 tereško 58.4k25 gold badges100 silver badges150 bronze badges asked Jul 9, 2013 at 13:32 TBATBA 1,1875 gold badges46 silver badges83 bronze badges 2- 1 You may use jQuery to manage onclick event to href. Inside `<a> tag you can store bookID in attribute. JavaScript event function simply read this, show confirm dialog and redirect to action after confirmation. – Robert Skarżycki Commented Jul 9, 2013 at 13:39
- Thanks Robert got it worked, miss placed the onclick inside the object attributes (please see Darins and Ryan's answers). However would like to confirm that, in chrome and all after few clicks we can disable alerts. What happens after that? – TBA Commented Jul 9, 2013 at 13:49
2 Answers
Reset to default 15You'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>