tags:

views:

491

answers:

1

Anyone have any examples of javascript actionresults? I am having a hard time getting the script to execute once it has been returned. Thanks

+2  A: 

Here's an example I found on a blog post, which actually describes it as an anti-pattern, because the Controller has to have in-depth knowledge of the View in order to function.

public ActionResult DoSomething() {   
    script s = "$('#some-div').html('Updated!');";   
    return JavaScript(s);   
}
Joseph
Perfect, thanks.
NP, welcome to SO!
Joseph
Just wanted to add for those viewing this in the future. It appears to me that the javascript result will only be executed when the action is called via ajax. Calling this action via an Html.Actionlink will result in the text of the script being returned, but not executed.