ASP.Net MVC run JavaScript in PartialView when loaded using Ajax.ActionLink - asp.net-mvc

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

Related

Rendering a partialview from within a helperclass in Umbraco

I have some html that is used a lot in the site i'm building. So I created a App_Code\Helpers.cshtml file and placed the helperfunction in that file.
Now, I want to render a partial-view (a MVC view for a form). But I can't use #Html.Partial("MyFormPartial", new formModel())
I can't find any other ways of rendering a partial view from within a helper class. Anybody got an idea on how to solve this?
Is a seperate helpers.cshtml even the best way for this kind of repeating html-code? I think it gives me a bit more freedom in the parameters I'm providing, instead of the macro's. But it sucks I can't use #Umbraco (without creating your own helper) or #Html :(
Just pass #Html to the helper function if you don't want to create it inside the helper function.
Nevertheless, isn't it a better idea to use a child action and render part of code you'd like to been shared?

ASP.NET MVC AJAX actionlink injects onclick attribute

I have a partial view which outputs a list of editors. Each editor in the list has a delete link, which is called using #Ajax.actionlink, like this:
if(Model.Any()){
<ul>
#foreach (var editor in Model)
{
<li>#editor.firstName #editor.lastName, #editor.emailAddress (#Ajax.ActionLink("Delete", "DeleteEditor", "EditorSurface", new { editorId = editor.id }, new AjaxOptions()))</li>
}
</ul>
}
I've included #using System.Web.Optimization; in my view and am calling the jquery unobtrusive ajax script from my view too:
#Scripts.Render("~/scripts/jquery.unobtrusive-ajax.min.js")
The issue is that when I view source, the rendered mark-up for my editor list items is like this:
<li>John Smith, johnsmith#example.com (Delete)</li>
I was a little disappointed to see the inline JavaScript in the delete links and was hoping for something a little 'cleaner' (possibly data attributes or something?). It strikes me this is going to be difficult to hook into (to provide confirmation messages etc).
Question is, is this the expected output of AJAX in ASP.NET MVC? I'm very new to this so not sure if I'm doing something wrong in my implementation.
Many thanks.
Yes, this is the markup the Ajax.* family of helpers produces. My recommendation in general, and especially in light of the fact that you don't want the on* attributes added, is to avoid these helpers like the plague. You'll have much cleaner and maintainable (though slightly more verbose) code handling AJAX yourself, i.e. just add a standard Html.ActionLink and then use jQuery to attach an event handler that calls the AJAX. Plus, when something goes wrong, as it almost inevitably does, you have full knowledge and control over the JavaScript that is running, rather than being at the mercy of some opinionated Microsoft dev.

how to implement progress bar for function in service and controller to show the status in grails platform

I am working on grails. I have a controller it calls service to check the value and one more call to do some calculation, by the time i want user to know its processing the function. Since i am new to grails , i dont know how to implement this in my project. I tried few plugins but i didnt know how to approach this by using plugin(Step by step). I need easiest way to solve this problem even this my ajax or javascript. i searched online i didnt get one.
Please help me to solve this .
Thank you.
Grails by default allows you to call controller methods with Ajax by default, you simply need to use the remoteFunction tag (http://grails.org/doc/2.2.1/ref/Tags/remoteFunction.html).
You use this by specifying an element on your page which will be updated by the html that you controller function returns, usually a gsp template.
For example, you could have your controller call the service to check for progress, and then the controller passes this returned progress amount to your gsp template. You use this value to draw a percentage, and return that html to your original page (this is whatever the controller returns - the remoteFunction tag handles the updating for you).
I suggest having a look for some examples of the remoteFunction call and going from there.
Hope that helps.

telerik mvc combobox copy and initialize

I am using a Telerik combobox and using jquery to make a clone of it.
the control is being rendered correctly, however the dropdown is not working I believe due to the fact the javascript has not been initialized on the control.
Is there a way to do this or a better way to make a duplicate of the control?
I may have to resort to making an ajax request but would ideally like to keep it all clientside.
Cheers,
Mark
After the clone you need to attach the AJAX call to it as follows.
var c = $('rowTemplate').clone();
c.find('selectorToGetInputControl').tAutoComplete({ ajax: { "selectUrl": "Your Ajax Call" },filter: 1 });
Not sure how you attempt to make a client clone of MVC server control using jQuery, I doubt such thing can operate properly. Either use another instance of the combo or go for pure client-side component.

Ajax.BeginForm in ASP.MVC2

I'd like to capture submit action from "Ajax.BeginForm (...)" and asynchronously fetch some data from controler and put it into some div...
How can i do it?
I've tried
Ajax.BeginForm(..., new AjaxOption( UpdateTriggerId = "", ) but as i noticed it is used for online checking form or something like this...
How can i disable reload whole page while pressing "submit" ?? Mayby i have add something to controler?
Btw. What is better to use with forms (in AJAX context)? Pure Jquery or those Ajax.BeginForm?
You should be able to achieve what you need using Ajax.BeginForm and with a partial view...
http://davidhayden.com/blog/dave/archive/2009/05/19/ASPNETMVCAjaxBeginForm.aspx

Resources