ASP.NET AJAX.BeginForm sends multiple requests
Asked Answered
C

3

16

Im relatevely new with Asp.net MVC3,

I have a form handled with Ajax, like this:

@using (Ajax.BeginForm("dtjson", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "detalle_tarifa", OnSuccess = "exito2", OnBegin="bloquear" }))

My problem is that when I submit data, it sends the request twice.

Lets take a look at the view.

This is the form:

enter image description here

ok, now the view after submit:

enter image description here

And this one is to show firebug debugging:

enter image description here

On the layout, I have those javascript files..

   <script src="@Url.Content("~/Scripts/jquery-ui.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.windows-engine.js")" type="text/javascript"></script>
       <script src="@Url.Content("~/Scripts/jquery.jqDock.js")" type="text/javascript"></script>
       <script src="@Url.Content("~/Scripts/jquery.jqDock.min.js")" type="text/javascript"></script>
       <script src="@Url.Content("~/Scripts/jquery.tablePagination.0.4.js")" type="text/javascript"></script>
       <script src="@Url.Content("~/Scripts/jquery.tools.min.js")" type="text/javascript"></script>
       <script src="@Url.Content("~/Scripts/jquery.cookie.js")" type="text/javascript"></script>
       <script src="@Url.Content("~/Scripts/jquery.treeview.js")" type="text/javascript"></script>
       <script src="@Url.Content("~/Scripts/jquery.treeview.edit.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

And on the view (A Partial View), I have this:

<script src="@Url.Content("~/Scripts/jquery-1.4.4.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui.js")" type="text/javascript"></script>
<script type="text/javascript" [email protected]("~/Content/tinymce/jscripts/tiny_mce/jquery.tinymce.js")></script>
<script type="text/javascript" [email protected]("~/Content/tinymce/jscripts/tiny_mce/tiny_mce.js")></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Href("~/Scripts/jquery.uploadify.js")" type="text/javascript"></script>
<script src="@Href("~/Scripts/jquery.scrollTo.js")" type="text/javascript"></script>
<script src="@Href("~/Scripts/jquery.blockUI.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="/Scripts/jquery.cookie.js" type="text/javascript"></script>
<script src="/Scripts/jquery.treeview.js" type="text/javascript"></script>
<script src="/Scripts/jquery.treeview.edit.js" type="text/javascript"></script>

It seems like it's a problem with jquery... but Im not sure of it...

Any help would be great, Thanks everybody!

Caudal answered 13/12, 2011 at 21:19 Comment(2)
Can post which javascript files are included in your view/layout?Pettifogger
of course I already did it! I found this could be a problem with jquery files..Caudal
P
66

You have included jquery.unobtrusive-ajax.min.js twice once in the layout once in the partial. So your browser executes the js inside twice which will subscribe twice on the form click event that is why doing two POST instead of one.
So you need to remove the jquery.unobtrusive-ajax.min.js from the partial.

Note: If your are using a partial with a layout you don't need to duplicate the js included in the partial because it's already done by the layout. There are some good articles about layouts and partials.

Pettifogger answered 14/12, 2011 at 12:54 Comment(5)
Ok, if I do not duplicate js'.. they will not work on the page.. I already tried it. Including the unobstrusive, by the way I removed it ant this time, it sends 3 requests...Caudal
I think the problem has to do with bugs.jquery.com/ticket/4373, unless the server code works fineCaudal
Thank you Nemesv. This helped me solve a problem I have been stuck on for nearly 2 days!!Herwick
Thank you soooooo much! this was driving me mental!!Rectangular
In my case I by mistake appended second JS after ajax operation. So make sure to inspect page in browser just prior postback that causes double server call.Jarlathus
G
5

You have added jquery.unobtrusive-ajax.js three times. It had happened to me as well.

Check in bundle config "~/ui/js/jquery.unobtrusive.*". the * is wildcard to match both minified or un-minified version. As your solution contains both of the files, it is adding "jquery.unobtrusive-ajax.js" and "jquery.unobtrusive-ajax.min.js". Again in your view page or layout page, you have added jquery.unobtrusive-ajax.js/jquery.unobtrusive-ajax.min.js which is making three times request/post

Germaun answered 7/9, 2017 at 8:40 Comment(0)
P
2

I my case I had "return View" instead of "return PartialView".

Penelopa answered 5/12, 2015 at 19:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.