38

How can one call a javascript function in html.actionlink in asp.net mvc?

I want to call one method, which is in JavaScript, but how I call it within html.actionlink in the same page?

starball
58.5k51 gold badges303 silver badges1k bronze badges
asked May 18, 2010 at 9:44
3
  • To me, it is not clear what you're trying to achieve. Html.ActionLink runs on the server, your javascript runs on the client; there is no way the one can call the other. Do you mean you want some piece of javascript to be executed when the link that is created by Html.ActionLink is clicked? Commented May 18, 2010 at 9:46
  • yes i want to call one method when clicked on particular link Commented May 18, 2010 at 11:13
  • It's worth noting that if your link is ONLY triggering some javascript, i.e a popup, and you dont actually want the user to navigate somewhere, then ActionLink is not the element you need. Standard href would be best for that scenario. Commented May 16, 2018 at 13:17

6 Answers 6

69

you need to use the htmlAttributes anonymous object, like this:

<%= Html.ActionLink("linky", "action", "controller", new { onclick = "someFunction();"}) %>

you could also give it an id an attach to it with jquery/whatever, like this:

<%= Html.ActionLink("linky", "action", "controller", new { id = "myLink" }) %>
$('#myLink').click(function() { /* bla */ });
answered May 18, 2010 at 9:49
Sign up to request clarification or add additional context in comments.

3 Comments

will the first, actionlink still call to the controller? if so how would i disable it?
@Djeroen, okay i'm an idiot, why would i even need an actionlink if i dont want it to call to a controller
Can we achieve the same thing with Ajax.ActionLink?
27

For calling javascript in your action link you simply need to write actionlink like this:

@Html.ActionLink("Delete", "Your-Action", new { id = item.id },
 new { onclick="return confirm('Are you sure?');"})

Don't get confused between route values and the html attributes.

pb2q
59.9k19 gold badges150 silver badges152 bronze badges
answered Mar 20, 2013 at 5:12

Comments

8
<a onclick="MyFunc()">blabla..</a>

There is nothing more in @Html.ActionLink that you could utilize in this case. And razor is evel by itself, drop it from where you can.

answered Oct 6, 2015 at 21:28

2 Comments

to me, this is the best answer!
Agreed. Based on just wanting to run a Javascript function, this is the more correct answer. Use an ActionLink when wanting to call the Controller, use the above to plainly call a JavaScript Function.
5
@Html.ActionLink("Edit","ActionName",new{id=item.id},new{onclick="functionname();"})
alecxe
476k127 gold badges1.1k silver badges1.2k bronze badges
answered May 16, 2013 at 10:27

Comments

4

This is a bit of an old post, but there is actually a way to do an onclick operator that calls a function instead of going anywhere in ASP.NET

helper.ActionLink("Choose", null, null, null, 
 new {@onclick = "Locations.Choose(" + location.Id + ")", @href="#"})

If you specify empty quotes or the like in the controller/action, it'll likely add a link to what you listed. You can do that, and do a return false in the onclick. You can read more about that at:

What's the effect of adding 'return false' to a click event listener?

If you're doing this onclick in an cshtml file, it'd be a bit cleaner to just specify the link yourself (a href...) instead of having the ActionLink handle it. If you're doing an HtmlHelper, like my example above is coming from, then I'd argue that calling ActionLink is an okay solution, or potentially better, is to use tagbuilder instead.

answered Jan 24, 2018 at 22:17

Comments

1

This is the only one that worked for me in .cshtml file:

@Html.ActionLink(
 "Name", 
 "Action", 
 "Controller", 
 routeValues: null, 
 htmlAttributes:new Dictionary<string, object> {{ "onclick", "alert('Test');" }})

I hope this helps.

answered Aug 23, 2017 at 17:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.