最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Confirmation for Url.Action in razor - Stack Overflow

programmeradmin3浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 15

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>
发布评论

评论列表(0)

  1. 暂无评论