Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confirmation for Url.Action in razor

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?

like image 487
TBA Avatar asked Jul 09 '13 13:07

TBA


2 Answers

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>
)
like image 84
Darin Dimitrov Avatar answered Nov 07 '22 06:11

Darin Dimitrov


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>
like image 5
Ryan Weir Avatar answered Nov 07 '22 06:11

Ryan Weir