Html.RenderPartial和Ajax.BeginForm - >提交被调用两次 [英] Html.RenderPartial and Ajax.BeginForm -> Submit is called twice
问题描述
我有以下索引视图:
@model BoringStore.ViewModels.ProductIndexViewModel
@{
ViewBag.Title = "Index";
}
<h2>Produkte</h2>
<div id='addProduct'>
@{ Html.RenderPartial("Create", new BoringStore.Models.Product()); }
</div>
<div id='productList'>
@{ Html.RenderPartial("ProductListControl", Model.Products); }
</div>
在productList的仅仅是一个所有产品清单。
The "productList" is just a list of all products.
该addProduct命令使我创建视图:
The addProduct renders my Create View:
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<div id="dialog-confirm" title="Produkt hinzufügen" style="display:none">
@using (Ajax.BeginForm("Index_AddItem", new AjaxOptions { UpdateTargetId = "productList" }))
{
@Html.Raw(DateTime.Now.ToString());
<div>
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
</div>
<br />
<div>
@Html.LabelFor(model => model.Price)
@Html.EditorFor(model => model.Price)
</div>
<br /><br />
<div>
<input type="submit" value="Produkt hinzufügen" />
</div>
}
当提交表单,在我的控制器Index_AddItem-方法被调用。不幸的是,窗体始终调用该方法的两倍。 (
When submitting the form, the Index_AddItem-method in my controller is called. Unfortunately the form always calls the method twice. :(
有人能帮助我吗?
推荐答案
不包括你的局部视图里面的脚本!他们应该去你的主的看法或你的 _layout.cshtml
。
Don't include scripts inside your partial views! They should go the your "main" views or your _layout.cshtml
.
您的问题是,你已经包含了 jquery.unobtrusive-ajax.min.js在你的页面
的两倍。一旦在你的创建
部分,一旦别的地方。因为如果包括脚本多次将认购提交事件上多次,所以你会得到多个提交一个单一的点击。
Your problem is that you have included the jquery.unobtrusive-ajax.min.js
twice in your page. Once in your Create
partial and once somewhere else. Because if you include that script multiple times it will subscribe on the submit event multiple times so you will get multiple submit with a single click.
因此,请确保您有包含脚本只在一个页面一次。因此移动 jquery.unobtrusive-ajax.min.js
引用到你的索引视图。
So make sure that you have include that script only once in a page. So move the jquery.unobtrusive-ajax.min.js
reference into your index view.
这篇关于Html.RenderPartial和Ajax.BeginForm - &GT;提交被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!