jQuery 对话框 + ajax - 第二次提交时表单数据重复,奇怪的行为?

发布于 2024-12-08 22:11:59 字数 2003 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朱染 2024-12-15 22:11:59

我以某种方式通过改变流程来“解决”它。我将 ShopList 转换为视图,每次用户添加项目/删除项目时,我都会使用另一个 _ShopList 部分视图刷新 div。

    $createdialog.dialog("option", "buttons", {
        "Cancel": function () {

            $createdialog.dialog('close');

        },
        "Submit": function () {

            var frm = $('#shopform');
            $.ajax({
                url: '/Shop/AddItem',
                type: 'POST',
                data: frm.serialize(),
                success: function (result) {
                    $('#shopfoods').html(result);
                    $createdialog.dialog('close');
                }
            });

        }
    });

这有效,尽管我不太确定为什么会这样。只是分享我的方式并希望在这里得到一些反馈...谢谢!

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.

    $createdialog.dialog("option", "buttons", {
        "Cancel": function () {

            $createdialog.dialog('close');

        },
        "Submit": function () {

            var frm = $('#shopform');
            $.ajax({
                url: '/Shop/AddItem',
                type: 'POST',
                data: frm.serialize(),
                success: function (result) {
                    $('#shopfoods').html(result);
                    $createdialog.dialog('close');
                }
            });

        }
    });

This works, although I am not really sure why is it. Just to share my way and hope get some feedback here... Thanks!!

逆蝶 2024-12-15 22:11:59

我遇到了同样的问题,经过几次尝试和错误后,我意识到罪魁祸首是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文