C# 如何在 mvc 控制器中创建确认框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15548073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:08:34  来源:igfitidea点击:

How to create the confirm box in mvc controller?

c#asp.net-mvcasp.net-mvc-3messagebox

提问by user279stack1

I need to create the confirm box in mvc controller?. Using this 'yes' or 'no' value I need to perform the action in my controller. How we do that?

我需要在 mvc 控制器中创建确认框吗?。使用这个“是”或“否”值,我需要在我的控制器中执行操作。我们怎么做?

Sample code:

示例代码:

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

采纳答案by TiagoBrenck

You dont create confirm box in a Controller, but yes in a View, using JQuery Dialog. The Controller is already inside the server, so you don't have user interactions there. Your View, in the other hand, is the place where the user will choose options, type information, click on buttons etc... You can intercept the button click, to show that dialog, and only submit the post when the option "Yes" gets clicked.

您不在控制器中创建确认框,但在视图中创建确认框,使用 JQuery 对话框。控制器已经在服务器内部,因此您在那里没有用户交互。另一方面,您的视图是用户选择选项、键入信息、单击按钮等的地方……您可以拦截按钮单击以显示该对话框,并且仅在选项“是”时提交帖子"被点击。

JQuery Dialog requires jquery.js, jquery-ui.js, jquery.ui.dialog.jsscripts referenced in your page.

JQuery Dialog 需要页面中引用的jquery.jsjquery-ui.jsjquery.ui.dialog.js脚本。

Example:

例子:

$(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');
                }
            }
        });
    });
});

回答by AliR?za Ad?yah?i

You can do this with ActionLink

你可以用 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?')" })

If user confirm, then link parameter will pass to the controller action method.

如果用户确认,则链接参数将传递给控制器​​操作方法。

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();
}

Update

更新

You can not show message box in controller side. But you can do this like following

您不能在控制器端显示消息框。但是你可以像下面这样做

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

     return View();
}

And view

并查看

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

But these all codes are not elegant way. This is solution of your scenerio.

但是这些所有代码都不是优雅的方式。这是您的场景的解决方案。

回答by Karthik Chintala

Yes, you can do this with @Html.ActionLinkas AliR?za Ad?yah?i has commented.

是的,你可以@Html.ActionLink像 AliR?za Ad?yah?i 评论的那样做到这一点。

Subscribe to the onclickevent of the @Html.ActionLink

订阅onclick事件@Html.ActionLink

Here is the implementation:

这是实现:

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

And in javascript write the confirmbox.

并在 javascript 中编写该confirm框。

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

Edit

编辑

Try like this:

像这样尝试:

<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" })

Now in your controller do some operations based on the isTruequerystring

现在在您的控制器中根据isTrue查询字符串执行一些操作

public ActionResult Somemethod(bool isTrue)
        {
            if (isTrue)
            {
                //do something
            }
            else
            {
                //do something
            }
            return View();
        }

回答by Maxcelcat

I can confirm that AliR?za Ad?yah?i's solution works well.

我可以确认 AliR?za Ad?yah?i 的解决方案运行良好。

You can also customize the the message. In my case we're using MVC and Razor, so I could do this:

您还可以自定义消息。就我而言,我们使用的是 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>

Which showed a dialog with a specific record named in it. Might also be possible to give the confirm dialog a title, haven't tried that yet.

其中显示了一个对话框,其中包含一个特定的记录。也可以给确认对话框一个标题,还没试过。

回答by Maxcelcat

  <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