2

I looked at other question: how to call javascript function in html.actionlink in asp.net mvc? showing how to add onclick to @Html.ActionLink but in my case function I assigned to onclick is never invoked.

I tried these action links:

<p>@Html.ActionLink("Call number", "Call", new { id = Model.Id, number = Model.CellNumber, onclick = "javascript:alert(a);" }, new { @class = "btn btn-default" })</p>
<p> @Html.ActionLink("Call number ยป", "Call", new { id = Model.Id, number = Model.SecondaryPhoneNumber, onclick = "javascript:Call();" }, new { @class = "btn btn-default" })</p>

and the function looks like:

<script>
    function Call(){
        alert("BLA");
    }
</script>

but no alert is shown in both cases(first and second button). Besides that action link wroks.

Question: What I did wrong?

EDIT:

Action links in html look like that and the look corrupted:onclick=Call%28%29%3B

<p><a class="btn btn-default" href="/Person/Call/7?number=113-456-789&amp;onclick=alert%28a%29%3B">Call Cell Phone</a></p>
<p> <a class="btn btn-default" href="/Person/Call/7?number=98873213&amp;onclick=Call%28%29%3B">Call Client&#39;s Secondary Number &#187;</a></p>

and the function looks like it should:

<script>
    function Call(){
        alert("BLA");
    }    
</script>
Community
  • 1
  • 1
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 1
    you dont need to add `javascript:` to an onclick. You would only do this for a `href` target on a link. e.g. either `onclick="Call()"` or `href="javascript:Call()"`. You can see this in the answer to [http://stackoverflow.com/questions/2856071/how-to-call-javascript-function-in-html-actionlink-in-asp-net-mvc] โ€“ Rhumborl Sep 06 '14 at 18:29
  • @Rhumborl This is the answer I used before I asked question here and did not work. Please see my edit it looks like that html browser sees gets something like that: `onclick=Call%28%29%3B` โ€“ Yoda Sep 06 '14 at 18:37
  • @JLRishe I posted HTML in EDIT at the bottom of OP it seems the name of function is corrupted there. I added link to the answer I used to solve the problem before I asked question here. โ€“ Yoda Sep 06 '14 at 18:39

1 Answers1

6

You add onclick handler in wrong place. You must put it in Html Attributes block instead Route values. Try it:

<p>@Html.ActionLink("Call number", "Call", new { id = Model.Id, number = Model.CellNumber }, new { @class = "btn btn-default", onclick = "alert('a');" })</p>
mykhailovskyi
  • 680
  • 1
  • 8
  • 19