3

I have a cshtml view in an MVC ASP.NET application. I want to simply allow the user to click a button and provide a warning to the user before the action is carried out. The only way I know to acheive what I want is to use an AjaxActionLink like so

<a role="button" 
   class="btn btn-lg btn-primary" 
   href='@Ajax.ActionLink(
        "Clear Invite Log",
        "ClearInviteLog",
        "Tools",
        null,
        new AjaxOptions { HttpMethod = "GET" },
        new { onclick = "return confirm('Are you sure you want to clear the invite log?');" })'>
    Clear Invite Log
</a>

Where the name of my controller is ToolsController and the method I am posting to is ClearInviteLog. The problem is that when I click the button I don't get the warning dialog, instead I get

Server Error in '/' Application. A potentially dangerous Request.Path value was detected from the client (<).

Stack Trace: [HttpException (0x80004005): A potentially dangerous Request.Path value was detected from the client (<).] System.Web.HttpRequest.ValidateInputIfRequiredByConfig() +9693412 System.Web.PipelineStepManager.ValidateHelper(HttpContext context) +53

I am new to web development and I am confused. What I am doing wrong here?

Thanks for your time.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • 1
    Use a data-binding library for your front end rather than Razor to auto generate you stuff, then you will find you will do less moulding, and hit less issues – Callum Linington Jun 11 '15 at 14:51
  • Check this question, it might be the same as yours http://stackoverflow.com/questions/5967103/a-potentially-dangerous-request-path-value-was-detected-from-the-client – bateloche Jun 11 '15 at 14:53
  • I have read that question. It is not the "same" as mine. Thanks for your time. – MoonKnight Jun 11 '15 at 15:05

1 Answers1

2

You do not need to set Ajax.ActionLink of anchor tag href value, Ajax.ActionLink and Html.ActionLink itself gets render as anchor tag, you just have to use Ajax.ActilnLink directly:

@Ajax.ActionLink(
    "Clear Invite Log",
    "ClearInviteLog",
    "Tools",
    null,
    new AjaxOptions { HttpMethod = "GET" },
    new 
       { 
         onclick = "return confirm('Are you sure you want to clear the invite log?');",
         @class ="btn btn-lg btn-primary",
         role="button" })
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160