Does anyone know what I need to get the ajax.beginform
to work in my MVC4 project? I updated the jQuery library to version 2 and I have included the jquery.validate.js
and jquery.validate.unobtrusive.js
but the form is still posting to the new page rather than just updating the UpdateTargetId
element
I have had a google and a lot of people seem to be having problems with a script called Microsoft.jQuery.Unobtrusive.Ajax
but this isn't in my project. Do I need to install this, and if so do I then need to do a find and replace on live
so that it uses on
instead?
This is the form code:
@using (Ajax.BeginForm("AddImage", "Quote", FormMethod.Post, new AjaxOptions { UpdateTargetId = "Files" }, new { enctype = "multipart/form-data" }))
{
}
This posts to the quote controller addimage action and all that does is write out a string to the screen (which should appear in the Files
div) but instead of doing an ajax call it is actually going to the Quote/AddImage page
First, you're missing InsertionMode in your AjaxOptions.
Second, it seems you're trying to upload files using Ajax form? You won't be able to. Please see this post, for example.
Finally, Ajax.BeginForm is using jQuery.live() function, which has been removed as of jQuery version 1.9. See this question for more info.
You should install jQuery Migrate Plugin which will restore deprecated features. This will at least restore your Ajax form functionality. Alas, it won't help you upload files through Ajax. But that's a different topic.
All I had to do in the end was download the latest jquery.unobtrusive-ajax.js
through nuget. This has been updated so it works with jquery 2