简体   繁体   English

带有Bootstrap / jQuery / ASP.MVC的确认对话框

[英]Confirmation Dialog with Bootstrap/jQuery/ASP.MVC

I have a simple table with records and each of them has a btn with data-id attribute to run the confirmation dialog: 我有一个带有记录的简单表,每个表都有一个带data-id属性的btn来运行确认对话框:

@foreach (var dog in Model)
        {
            <tr>
                <td>@dog.Name</td>
                <td>@dog.Age</td>
                <td><a href="#" data-id="@dog.Id" class="btn btn-danger deleteBtn">Delete</a></td>
            </tr>
        }

After clicking the delete btn, this code is running : 单击删除btn之后,此代码正在运行:

$('.deleteBtn').on('click', function () {
            $.ajax({
                url: '@Url.Action("DeleteConfirm", "Home")',
                data: {
                    id : $(this).attr('data-id')
                },
                success: function(data)
                {
                    $('#myModal').empty().html(data).modal('show');
                }
            });
        });

As you can see, its Ajax request to my Action in HomeController. 如您所见,它对HomeController中我的Action的Ajax请求。 It returns PartialView that is loaded to my Bootstrap dialog: 它返回PartialView,该视图已加载到我的Bootstrap对话框中:

<div class="modal fade" id="myModal">
</div>

After showing the dialog, the user can click confirm to delete the row via button with class saveBtn. 显示对话框后,用户可以单击带有类saveBtn的按钮确认以删除行。 Thats what happens after I click it : 那就是我点击它之后发生的事情:

$('#myModal').on('click', '.saveBtn', function () {
            var numer = $(this).attr('data-id');

            $.ajax({
                url: '@Url.Action("DeleteDog", "Home")',
                type: 'POST',
                dataType: 'JSON',
                data: {
                    id: numer
                },
                success: function (data)
                {
                    if (data = 'true')
                        $('a[data-id=' + numer + ']').closest('tr').remove();
                }
            });
        });

So as you can see there is another Ajax (POST this time) request to delete the row. 如您所见,还有另一个Ajax(这次是POST)请求删除该行。

So here is my question. 所以这是我的问题。 Is this approach good practice? 这是好的做法吗? I'm not sure because in order to delete a row i have to send 2 ajax request (to load confirm dialog, and delete after confirm). 我不确定,因为要删除一行,我必须发送2个a​​jax请求(以加载确认对话框,并在确认后删除)。 Can anyone experience developer comment this code and say whether im doing it good or bad? 经验丰富的开发人员都可以评论此代码,然后说出即时消息是好是坏。 I would be grateful. 我会很感激。

It's always a good idea to challenge whether or not you could do things more effectively (as you have here.) So, let's think about it... 挑战您是否可以更有效地做事情(就像您在这里一样)总是一个好主意。所以,让我们考虑一下...

The Question: 问题:

Is it a good approach to send to two ajax requests - one to load a partial view and the second to post a delete request from the partial - for a single "user action"? 对于单个“用户操作”,发送两个ajax请求(一个加载部分视图,第二个从部分视图发布删除请求)是一种好方法吗?

The Answer: 答案:

Maybe. 也许。

Why? 为什么?

Based your example, if your goal is to simply confirm with the user whether or not they want to "Delete Dog ID: foo ?" 根据您的示例,如果您的目标是仅与用户确认他们是否要“删除Dog ID: foo ?”, You can absolutely do that without making a trip to the server for a partial view. 您绝对可以做到这一点,而不必前往服务器进行部分查看。 Presumably, in your first ajax call - $(this).attr('data-id'); 大概在您的第一个ajax调用中- $(this).attr('data-id'); - is the dog's Id. -是狗的ID。 You already have it! 您已经拥有了!

In most circumstances, when you make a get request with an identifier, like /dogs/id?='foo' you're using foo to access some additional information about the object from a persistent data store. 在大多数情况下,当您发出带有标识符的get请求时,例如/dogs/id?='foo'您就使用foo从持久性数据存储中访问有关该对象的一些其他信息。 Like, for example, the dog's owners, similar dog breeds, last visit to the vet, etc. 例如,狗的主人,类似的犬种,最后一次看兽医等等。

If additional information about the dog (owners, other dogs, vet) is an important part of deciding whether or not to delete the dog AND there's a good reason not to load it when the page loads intially then it totally makes sense to issue two ajax calls. 如果有关狗(所有者,其他狗,兽医)的其他信息是决定是否删除该狗的重要部分, 并且 有充分的理由在页面初始加载时不加载它,那么发出两个ajax完全有意义电话。

It could also make sense to issue two ajax calls just to get the partial view html, even if you don't need additional "dog" data, if you simply don't want the "parent" page to be to big (hence, slowing it's loading time.) 即使您不需要“父”页面很大(即使这样,如果您不需要“父”页面也很大),发出两个ajax调用也只是为了获取部分视图html是有意义的。减慢加载时间。)

Ultimately, though if you're not retrieving additional data, and there's no specific reason to load additional html, it's probably best in this case to just update the modal dialog using javascript/jquery: 最终,尽管如果您不检索其他数据,并且没有特定的原因来加载其他html,则在这种情况下最好只使用javascript / jquery更新模式对话框:

Example fiddle 小提琴的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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