Look at the redefinition of the OnActionExecuted base controller, which should give you access to the view model after it has been processed by the action. I'm curious how exactly are you going to inject a javascript snippet into the AJAX response, which is usually a simple JSON object?
If you are really asking how to add javascript from the controller, you can specify the following:
<script type="text/javascript" defer="defer">
@Html.Raw(ViewBag.StartupScript)
</script>
You can add this above to a specific view or layout page. Then you can do something like this:
public class MyController : Controller
{
public override void OnActionExecuted(...)
{
if (...)
{
ViewBag.StartupScript = "alert('hello world!');";
}
}
}
Chris source
share