Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi In ASP.net MVC
I Have a method in controller
from this method i have to call the javascript function in client side
How to call the javascript function from MVC controller method
Posted
Comments
Bun Leap_kh 21-Feb-12 3:41am    
MVC Controller is on Server side. It can't call javascript on client side. In contrast, javascript could request to MVC Controller!
[no name] 21-Feb-12 5:14am    
Is there any way to call the javascript function from Server side..

I'm afraid you can't do this. You can't call Client side javascript function from Server side controller, AFAIK.
 
Share this answer
 
It can be done with SignalR, See Scott Hanselmans blog[^] on Asynchronous scalable web applications with real-time persistent long-running connections with SignalR

Regards
Piet
 
Share this answer
 
There are two things you can do, one of which is what you mean, unless you're terminally confused about client/server separation.


  1. You want to run some client-side javascript as soon as the controller method 'returns', i.e. when the page is served. Answer: put an onload handler on the body element, or run some inline script in the view if getting at the body tag is a pain (i.e. if you're using a master page.)
  2. Or: You are calling this controller method in an AJAX request, and want the client code to do something as a result of what you return. By definition, an AJAX request is called from within JS and you can hook a JS function to act as a response handler, so construct some output in the controller method or view which tells the client what to do, and write the client script so that it looks in the AJAX response and does whatever it's asked to.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900