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>
3
  • 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 [stackoverflow.com/questions/2856071/… Commented Sep 6, 2014 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 Commented Sep 6, 2014 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. Commented Sep 6, 2014 at 18:39

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.