Call js from MVC Controller
I have a web site written using ASP.NET MVC3.
The site has one single web page and what I want to do is to have the controller handle the request, do some parsing and processing and开发者_如何学JAVA thereafter trigger a certain javascript method in the single webpage based on part of the data from the request.How would I do this in the MVC controller?
You can't directly trigger actions in the View from the controller.
However, what you can do is return an ActionResult from the Controller Action to the View that instructs the View to execute your Javascript.
This could be a PartialView that includes the Javascript (in which case, you'll need a placeholder in your view for the PartialView to be rendered 'into') or something like a JSONResult that contains a property instructing the logic within the View what to do.
Either way, the call to the Controller Action is going to be triggered by client-side Javascript and your desired logic executed when the Controller Action finishes executing.
You'll probably find it easier to use jQuery.
精彩评论