15

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

示例代码:

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

5 回答 5

7

您可以使用 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>

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

于 2013-03-21T13:03:45.343 回答
6

@Html.ActionLink是的,正如 AliRıza Adıyahşi 所评论的那样,您可以这样做。

订阅该onclick事件@Html.ActionLink

这是实现:

@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();
        }
于 2013-03-21T13:04:27.630 回答
5

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

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');
                }
            }
        });
    });
});
于 2013-03-21T12:57:11.523 回答
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>

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

于 2013-10-23T01:04:04.423 回答
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

于 2018-07-31T09:59:54.007 回答