0

I need a confirm window before calling the action specified in Url.Action. My code is:

<a href="@Url.Action("Delete", "OriginalWorks", new {id = w.Id})" 
onclick="return confirm('Do you... '+ @w.Title + '?')"><span></span></a>

Where w is the instance of the OriginalWork class. If I look into HTML of the site, there is the right string value in confirm, but the window doesn't show and the action is called immediately. If a do the same with an int attribute, for example Id:

<a href="@Url.Action("Delete", "OriginalWorks", new {id = w.Id})" 
onclick="return confirm('Do you... '+ @w.Id + '?')"><span></span></a>

It works as expected, showing a message in an confirm window with an value of Id.

I tried Title.toString(), but it doesn't help. I think it would work with Html.ActionLink, but I want to use this because of span in a a element.

Any ideas? Thanks a lot.

Margarit
  • 107
  • 9

2 Answers2

0

Try to use HttpUtility.HtmlEncode for your w.Title to awoid escape chars that can break javascript

EkzoMan
  • 63
  • 2
  • 7
0

Thanks for help. Solved it now, I didn't realize that the w.Title value was evaluated as a variable (and JavaScript maybe tried to find a variable called like a Title value), not as a string value. It simply works like this:

<a href="@Url.Action("Delete", "OriginalWorks", new {id = w.Id})" 
onclick="return confirm('Do you... @w.Title?')"><span></span></a>

So there is an expected string value of Title.

Margarit
  • 107
  • 9