开发者

ASP.Net MVC run JavaScript in PartialView when loaded using Ajax.ActionLink

开发者 https://www.devze.com 2022-12-13 07:55 出处:网络
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.

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


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.


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.


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.


You can try Multipartials , you can use them to update multiple views and even run scripts from partials, im not sure about the exact specifics as its been a while, but you can have a look at it and see if it can accomplish what you are looking for

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号