Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've got a question regarding ASP.Net MVC.

I'm using an Ajax.ActionLink to load a PartialView.

In this partial view is a javascript function I'd like to get called.

However I can't figure out how to make this happen.

I've tried using AjaxOptions { OnSuccess="functionInPartialView" } when I set the Ajax.ActionLink but for some reason it can't see the Javascript.

EDIT: The PartialView is a mix of JavaScript and Html

share|improve this question
    
what do you mean that you can't see the javascript. It's not rendering in the page , or it is not called. –  J.W. Dec 3 '09 at 20:51
    
It's not being called. I've tried many different ways of trying to get it to fire. The only way that I found worked was to load the PartialView using a jquery ajax call –  sf. Dec 4 '09 at 11:02

4 Answers 4

up vote 5 down vote accepted

I would suggest to use jQuery ($.get/$.ajax). It evaluates the $(function(){}) when you load the partial, so your scripts there fire. And I personally find jQuery easier and cleaner to use.

share|improve this answer
    
yup, you're right. I've ended up resorting to this and it works sweet. cheers –  sf. Dec 4 '09 at 10:47

I'm having the same problem. If you look at the HTML injected in to the DOM via Ajax, then you'll see that the javascript isn't even there. It seems to get stripped out. I believe that this is a problem related to the way ASP.NET MVC injects the HTML from the partial in to the DOM. You can use a different method that won't strip out the script by writing your own jQuery Ajax calls. The problem for me is that I then lose all the nice unobtrusive Ajax validation magic.

share|improve this answer

Add the javascript code in the view that will contain the parcialview, and next use the ajaxOptions { OnSuccess="functionInView" } when you set the Ajax.ActionLink.

share|improve this answer

If it's only javascript in your partial view, then you should be using a JavaScript Action result, as in this post

Otherwise, the issue is that ajax merely loading content into a div doesn't mean that it executes. In your ajax callback, you need to find the javascript content and eval it, so that your page is aware of the function definition.

I know that's a high level description, but I don't have any samples of doing this. If you post some of the code, maybe someone can suggest a cleaner way of doing this so that you have better access to the script.

share|improve this answer
    
unfortunately it's not only javascript. Sorry I should have made that more clear –  sf. Dec 3 '09 at 15:31
2  
+1 to make up for whoever -1. Nothing wrong here. –  µBio Dec 3 '09 at 17:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.