MattyD
MattyD

Reputation: 175

ASP.net mvc Call function on link click

I want to be able to call a method from a link click. The situation is I have a chat client within my website. When the user clicks on the Open Chat client link I want to fire off a method that updates all the contacts the user is allowed to talk to and than it fires off javascript to open the chat client.

Ive got the javascript to open the chat client firing but not the method before. All the method will do is update some database records that the chat client accesses (I only want to do this if they open the chat client otherwise it is overkill and an unneeded perf hit).

Thanks in advance

Upvotes: 2

Views: 1064

Answers (1)

salgiza
salgiza

Reputation: 5900

The easiest solution is to use jQuery (or any other js library) to make an ajax call to your action method. Depending on how your app works, you can make the call asyncrhonously, or wait for your method to respond before opening the chat window.

If using jQuery, the only line of js needed is:

   $.get('/controller/method', function(response) { alert('db updated!') });

Upvotes: 2

Related Questions