【问题标题】:ASP.net & MVC : AJAX & update PartialView & confirmation messageASP.net & MVC : AJAX & 更新 PartialView & 确认消息
【发布时间】:2013-01-11 08:59:35
【问题描述】:

我正在使用 ASP.Net MVC 来制作我的应用程序

我实际上是在尝试从我的数据库中删除一个条目,首先我必须有一条确认消息,当操作完成后我想刷新我的 PartialView

这是我的代码

          <%= Ajax.ActionLink(
            "Supprimer", 
            "Delete", 
            "Users",
            new { item.ID },
            new AjaxOptions {
              Confirm= "confirmMethod",
              UpdateTargetId = "usersListeID",
              OnSuccess = "success"
           }
              )%>

问题是确认选项是一个简单的 ajax 消息,我想使用我自己的消息(结构良好,因为我使用 ajax 和 jQuery 来制作它)

function confirmMethod() {
$.msgBox({
    title: "Demande de confirmation",
    content: "Voulez-vous effectuer cette action?",
    type: "confirm",
    buttons: [{ value: "Oui" }, { value: "Non"}],
    success: function (result) {
        if (result == "Non") {
            abort();
        }
    }
});}

【问题讨论】:

标签: asp.net-mvc jquery confirmation asp.net-mvc-partialview


【解决方案1】:

您可以使用标准的Html.ActionLink

<%= Ajax.ActionLink(
    "Supprimer", 
    "Delete", 
    "Users",
    new { item.ID },
    new { @class = "delete" }
) %>

然后使用jQuery订阅tis锚的.click()事件:

$(function() {
    $('.delete').click(function() {
        var anchor = this;
        // Show the confirmation dialog
        $.msgBox({
            title: "Demande de confirmation",
            content: "Voulez-vous effectuer cette action?",
            type: "confirm",
            buttons: [{ value: "Oui" }, { value: "Non"}],
            success: function (result) {
                if (result == "Oui") {
                    // send the AJAX request if the user selected "Oui":
                    $.ajax({
                        url: anchor.href,
                        type: 'POST',
                        success: function(data) {
                            // When the AJAX request succeeds update the 
                            // corresponding element in your DOM
                            $('#usersListeID').html(data);
                        }
                    });
                }
            }
        });

        // Always cancel the default action of the link
        return false;
    });
});

【讨论】:

  • 感谢您的回答,但此解决方案会更新整个页面,我只想更新我的部分视图(ID 为#usersListeID)
  • 这就是我展示的代码的作用。如果 AJAX 调用成功,它将使用您的 Delete 操作返回的内容更新 #usersListeID 元素。因此,请确保此操作仅返回要更新的部分 HTML,而不是整个视图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
  • 2016-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多