简体   繁体   中英

Ajax.BeginForm posts twice if partial view is rendered with AJAX

Here is the scenario:

  1. A partial view is retrieved by some JavaScript (AJAX call to controller returning a partial view) and the HTML is added to the DOM
  2. The partial view contains a form rendered by Ajax.BeginForm()
  3. When the form is submitted (submit button clicked), it posts twice

If I render the partial view with the original view with Html.Partial() (instead of adding it using JavaScript) the form is only submitted once, as expected.

The script for adding the partial view looks like this:

$.ajax({
   type: 'GET',
   url: '/MyController/MyAction',
   cache: false,
   contentType: "application/html; charset=utf-8",
   dataType: 'html',
   success: function (result) {
      $('#body').append(result);
   }
});

Any ideas? :) Thanks!

I'm an idiot. My controller returned View instead of PartialView - so of course it contained all the JavaScript resources. In other words, problem was caused by double unobtrusive script links . :/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM