剃刀中Url.Action的确认

Confirmation for Url.Action in razor

本文关键字:确认 Action Url 剃刀      更新时间:2023-09-26

嗨,我在网格视图中使用编辑按钮。在调用操作之前,我想要一个确认按钮?

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属性并没有进行计算,而是作为一个参数传递。如何进行确认?

您把它放错了地方。现在,您已经将它作为参数传递给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>
)

通过将"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>