用剃刀确认Url.Action

时间:2013-07-09 13:32:02

标签: javascript asp.net-mvc razor

您好我在gridview中使用了一个编辑按钮。在打电话给行动之前,我想要一个确认按钮?

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>

但是onclick属性没有评估,而是作为参数传递。我怎样才能得到确认?

2 个答案:

答案 0 :(得分:13)

你把它放在了错误的地方。现在你已将它作为参数传递给Url.Action助手,而它应该是一个单独的属性,与定义href属性的方式相同:

<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>

顺便说一下,你应该考虑使用帮手:

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>
)

答案 1 :(得分:5)

通过将“onclick”放在Url.Action助手中,您告诉将其翻译为URL参数。

你要做的是将onclick放在帮助器之外,如下所示:

<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>