Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call javascript function in html.actionlink in asp.net mvc?

how to call javascript function in html.actionlink in asp.net mvc?

i wan to call one method which is in java script but how to call it within html.actionlink in same page thanks in advance

like image 796
Renu123 Avatar asked May 18 '10 09:05

Renu123


People also ask

How can we call JavaScript function on HTML ActionLink in ASP NET MVC?

ActionLink click in ASP.Net MVC Razor. When the ActionLink inside the WebGrid row is clicked, a JavaScript function will be called inside which the data from the WebGrid row will be fetched and displayed using JavaScript Alert Message Box.

What is HTML ActionLink ()?

Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.

What is difference between HTML ActionLink and URL action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.

How do I pass an object in ActionLink?

If you need to pass through the reference to an object that is stored on the server, then try setting a parameter of the link to give a reference to the object stored on the server, that can then be retrieved by the action (example, the Id of the menuItem in question).


2 Answers

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 */ }); 
like image 119
Andrew Bullock Avatar answered Sep 19 '22 04:09

Andrew Bullock


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.

like image 34
Ashwini Avatar answered Sep 17 '22 04:09

Ashwini