如何在MVC控制器中创建确认框?

16

我需要在MVC控制器中创建确认框?使用'yes'或'no'值,我需要在我的控制器中执行相应的操作。我们该如何实现?

示例代码:

    public ActionResult ActionName(passing value)
        {
             // some code 
             message box here
               if (true)
                     { true code}
              else { else code}
       }

你可以使用Html.ActionLink来实现这个功能... - AliRıza Adıyahşi
不。我在我的控制器中有一些值,我需要使用这些值执行某些操作。另一个选项:将请求发送到服务器端,并传递“是”或“否”的值给服务器,使用这些值我们将执行某些操作。 - user279stack1
1
@user279stack1 你尝试过什么?你写了什么代码? - Daniel A. White
也可以使用ActionLink完成。同时@DanielA.White是正确的... - AliRıza Adıyahşi
你不能在控制器端显示消息框,请查看我的更新答案... - AliRıza Adıyahşi
5个回答

8
你可以使用ActionLink来实现这个功能。
@Html.ActionLink(
    "Delete", 
    "DeleteAction", 
    "Product", 
    new { confirm = true, other_parameter = "some_more_parameter" }, 
    new { onclick = "return confirm('Do you really want to delete this product?')" })

如果用户确认,则链接参数将传递给控制器操作方法。
public ActionResult DeleteAction(bool confirm, string other_parameter)
{
    // if user confirm to delete then this action will fire
    // and you can pass true value. If not, then it is already not confirmed.

    return View();
}

更新

您无法在控制器端显示消息框。但是可以按照以下方式执行此操作:

public ActionResult ActionName(passing value)
{
     // some code 
     message box here
     if (true){ ViewBag.Status = true }
     else { ViewBag.Status = false}

     return View();
}

并查看

<script type="text/javascript">
function() {
    var status = '@ViewBag.Status';
    if (status) {
        alert("success");
    } else {
        alert("error");
    }
}
</script>

但这些代码并不是优雅的方式。以下是解决方案。

再加1。比我快42秒。 - Karthik Chintala

6

是的,你可以使用@Html.ActionLink来实现这个功能,就像AliRıza Adıyahşi所评论的那样。

订阅@Html.ActionLinkonclick事件

以下是实现方式:

@Html.ActionLink("Click here","ActionName","ControllerName",new { @onclick="return Submit();"})

在 JavaScript 中编写 confirm 对话框。

<script type="text/javascript">
function Submit() {
        if (confirm("Are you sure you want to submit ?")) {
            return true;
        } else {
            return false;
        }
    }
</script>

编辑

尝试如下:

<script type="text/javascript">
    function Submit() {
            if (confirm("Are you sure you want to submit ?")) {
                document.getElementById('anchortag').href += "?isTrue=true";
            } else {
                document.getElementById('anchortag').href += "?isTrue=false";
            }
            return true;
        }
</script>

@Html.ActionLink("Submit", "Somemethod", "Home", new { @onclick = "return Submit();", id = "anchortag" })

现在,在您的控制器中,根据 isTrue 查询字符串执行一些操作。
public ActionResult Somemethod(bool isTrue)
        {
            if (isTrue)
            {
                //do something
            }
            else
            {
                //do something
            }
            return View();
        }

5
您不应该在控制器中创建确认框,而是应该在视图中使用JQuery Dialog来创建。因为控制器已经位于服务器端,因此您无法在其中进行用户交互。相比之下,视图是用户选择选项、输入信息、点击按钮等的地方。您可以拦截按钮单击事件,以显示该对话框,并且只有在单击"Yes"选项时才提交帖子。
要使用JQuery Dialog,您需要在页面中引用jquery.jsjquery-ui.jsjquery.ui.dialog.js脚本。
示例:
$(function(){
    $("#buttonID").click(function(event) {
        event.preventDefault();
        $('<div title="Confirm Box"></div>').dialog({
            open: function (event, ui) {
                $(this).html("Yes or No question?");
            },
            close: function () {
                $(this).remove();
            },
            resizable: false,
            height: 140,
            modal: true,
            buttons: {
                'Yes': function () {
                    $(this).dialog('close');
                    $.post('url/theValueYouWantToPass');

                },
                'No': function () {
                    $(this).dialog('close');
                    $.post('url/theOtherValueYouWantToPAss');
                }
            }
        });
    });
});

不必使用 .live。您可以直接使用 .click(function(){...});。 - Tiago B
我需要在我的控制器中执行某些操作。 - user279stack1
我改变了按钮的功能。这是你需要的吗? - Tiago B
好的,我看到了,并且再次修改了这个函数。 - Tiago B

1
我可以确认AliRıza Adıyahşi的解决方案很有效。
您还可以自定义消息。在我的情况下,我们正在使用MVC和Razor,所以我可以这样做:
<td>
@Html.ActionLink("Delete", 
    "DeleteTag", new { id = t.IDTag }, 
    new { onclick = "return confirm('Do you really want to delete the tag " + @t.Tag + "?')" })
</td>

该对话框显示了一个特定记录的名称。可能还可以为确认对话框设置标题,但我尚未尝试过。


0
  <a href="@Url.Action("DeleteBlog", new {id = @post.PostId})" class="btn btn-sm btn-danger" onclick="return confirm ('Are you sure want to delete blog?');">
                                <i class="glyphicon glyphicon-remove"></i> Delete


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接