Confirmation for Url.Action in Razor

Hi, I am using the edit button inside gridview. Do I want a confirmation button before a call to 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 evaluated; instead, it is passed as a parameter. How can I get confirmation?

+4
source share
2 answers

You put it in the wrong place. Right now, you passed it as a parameter to the Url.Action helper, whereas it should be a separate attribute, just as 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 this:

 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> ) 
+9
source

By placing "onclick" inside the Url.Action helper, you tell him to translate it as a URL parameter.

Instead, onclick is called outside of the helper instead:

 <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> 
+4
source

Source: https://habr.com/ru/post/1490490/


All Articles