最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ajax.BeginForm posts twice if partial view is rendered with AJAX - Stack Overflow

programmeradmin1浏览0评论

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!

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!

Share Improve this question edited Apr 26, 2017 at 1:28 Ted Nyberg asked Oct 4, 2013 at 9:56 Ted NybergTed Nyberg 7,41110 gold badges44 silver badges82 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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. :/

发布评论

评论列表(0)

  1. 暂无评论