jQuery 对话框 + ajax - 第二次提交时表单数据重复,奇怪的行为?
我正在使用 MVC 3 和 jquery 对话框 n $.ajax。
我得到了 1 个带有“生成按钮”和脚本的视图(Index.cshtml),如下所示:
// Once generate is clicked, call GenerateList action
$('#generate').click(function () {
$.ajax({
async: false,
url: '/Shop/GenerateList',
type: 'GET',
success: function (result) { getShopListFood(result); }
});
});
getShopListFood 只是一个返回部分视图的控制器操作:
// Function to generate shopListFood items based on shopListID
public ActionResult GetShopListFood(int shopListID)
{
var shopListFood = (from f in dbEntities.SHOPLISTFOODs where f.ShopListID == shopListID select f).ToList();
return PartialView("_ShopList", shopListFood);
}
然后在部分视图中,我创建一个对话框来加载另一个部分视图,其中是创建新数据库实例的表单: var $createdialog = $('').load('/Shop/GetShopListFoodForm').dialog({ 自动打开:假, title: '添加新项目', 模态:真 });
$createdialog.dialog("option", "buttons", {
"Cancel": function () {
$createdialog.dialog('close');
},
"Submit": function () {
var frm = $('#formData');
$.ajax({
async: false,
url: '/Shop/AddItem',
type: 'POST',
data: frm.serialize(),
success: function (result) {
$('.shoplistfood').html(result);
$createdialog.dialog('close');
}
});
}
});
$('#additem').button().click(function () {
// Once clicked, create a dialog to load _ShopListFoodForm dialog
//clear();
$createdialog.dialog('open');
});
然而,我遇到了一个非常奇怪的情况。第二次创建将仅具有与第一次创建相同的数据。例如,我创建 ADD1 并提交,商店列表更新,然后我在表单对话框中输入 ADD2 并提交,我得到另一个 ADD1 POST 到操作而不是 ADD2。
我一直在我的应用程序中的许多其他模块中使用某种方法,但从未遇到过这样的问题。我只是不知道出了什么问题!
希望我清楚地说明了问题,并能在这里得到一些帮助……
真的很感激…………
I am using MVC 3 with jquery dialog n $.ajax.
I got 1 View (Index.cshtml) with a "Generate button" and script as below:
// Once generate is clicked, call GenerateList action
$('#generate').click(function () {
$.ajax({
async: false,
url: '/Shop/GenerateList',
type: 'GET',
success: function (result) { getShopListFood(result); }
});
});
The getShopListFood is simply a controller action that return a partial view:
// Function to generate shopListFood items based on shopListID
public ActionResult GetShopListFood(int shopListID)
{
var shopListFood = (from f in dbEntities.SHOPLISTFOODs where f.ShopListID == shopListID select f).ToList();
return PartialView("_ShopList", shopListFood);
}
Then at the partial view, I create a dialog to load another partial view, which is the Form to create a new db instance:
var $createdialog = $('').load('/Shop/GetShopListFoodForm').dialog({
autoOpen: false,
title: 'Add New Item',
modal: true
});
$createdialog.dialog("option", "buttons", {
"Cancel": function () {
$createdialog.dialog('close');
},
"Submit": function () {
var frm = $('#formData');
$.ajax({
async: false,
url: '/Shop/AddItem',
type: 'POST',
data: frm.serialize(),
success: function (result) {
$('.shoplistfood').html(result);
$createdialog.dialog('close');
}
});
}
});
$('#additem').button().click(function () {
// Once clicked, create a dialog to load _ShopListFoodForm dialog
//clear();
$createdialog.dialog('open');
});
However, I get a very weird situation. The 2nd create will just have the data duplicate as the 1st one. For example, I create ADD1 and submit, the shoplist updated, then I input ADD2 in the form dialog and submit, I get another ADD1 POST to the action instead of ADD2.
I had been using some method for many other module in my app but never meet such problem. I just don't know what is going wrong!
Hope I made myself clear about the problem, and can get some kind help here...
Really appreciate that..............
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我以某种方式通过改变流程来“解决”它。我将 ShopList 转换为视图,每次用户添加项目/删除项目时,我都会使用另一个 _ShopList 部分视图刷新 div。
这有效,尽管我不太确定为什么会这样。只是分享我的方式并希望在这里得到一些反馈...谢谢!
I somehow "solve" it by change the flow. I turn the ShopList into a View, and everytime user add item / delete item, i will refresh the div with another _ShopList partial view.
This works, although I am not really sure why is it. Just to share my way and hope get some feedback here... Thanks!!
我遇到了同样的问题,经过几次尝试和错误后,我意识到罪魁祸首是 jquery ui 对话框声明。对话对象在部分视图中声明为全局变量,在成功提交表单后重新加载。重新加载的部分视图将再次声明相同的全局变量,因此会造成混乱:您实际上再次提交了原始对象(仍在范围内)。
我知道我晚了几年,但这可能对将来遇到同样问题的人有用。
I had the same problem and after a few trial and errors I realized that the culprit was the jquery ui dialog declaration. The dialogue object was declared as a global variable in a partial view that got reloaded after a successful form submission. The reloaded partial view would declare the same global variable again and hence the mess: you're really submitting the original object - still in scope - again.
I know I'm a few years late, but this might be useful for anyone having the same problem in the future.