2013-03-21 54 views
8

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

示例代码:

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

你可以用Html.ActionLink ... – 2013-03-21 12:56:10

+0

做到这一点。我在我的控制器中使用这个值有一些价值,我需要执行一些操作。另一种选择:将请求发送到服务器端并将yes或no值传递给服务器使用此操作我们将执行一些操作 – user279stack1 2013-03-21 12:59:04

+1

@ user279stack1您尝试过什么?你做了什么代码? – 2013-03-21 13:00:59

回答

1

你不创建控制器确认框,但肯定在视图中,使用JQuery对话框。 控制器已经在服务器中,因此您没有用户干预。 您的意见,是用户将选择的地方选项,类型信息,点击按钮... 您可以拦截按钮点击,显示该对话框,并且只有当按钮“是”被点击时提交帖子。 JQuery对话需要(jquery.js,jquery-ui.js,jquery.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'); 
       } 
      } 
     }); 
    }); 
}); 
+0

.live不是必需的。你可以使用.click(function(){...}); – TiagoBrenck 2013-03-21 12:58:41

+0

我需要在我的控制器中执行一些操作\ – user279stack1 2013-03-21 13:00:43

+0

我改变了按钮功能。那是你需要的吗? – TiagoBrenck 2013-03-21 13:03:34

4

你可以,如果用户确认,然后链接参数将传递给控制器​​的操作方法与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> 

但所有这些代码是不优雅的方式。这是你的scenerio的解决方案。

+0

再次+1。比我快42秒 – 2013-03-21 13:04:58

+0

查看我的最新帖子 – user279stack1 2013-03-21 13:08:41

+0

如何在我的控制器中执行代码。 – user279stack1 2013-03-21 13:57:08

4

是的,你可以这样做@Html.ActionLinkAliRızaAdı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(); 
     } 
+0

检查我的最新帖子 – user279stack1 2013-03-21 13:08:58

+0

@ user279stack1检查编辑的帖子 – 2013-03-21 13:24:11

1
一些操作

我可以证实,AliRızaAdıyahşi的解决方案效果很好

您也可以自定义消息。在我的情况下,我们使用MVC和剃刀,所以我可以这样做:

<td> 
@Html.ActionLink("Delete", 
    "DeleteTag", new { id = t.IDTag }, 
    new { onclick = "return confirm('Do you really want to delete the tag " + @t.Tag + "?')" }) 
</td> 

这表明在它命名为特定记录的对话框。也可以给确认对话一个标题,还没有尝试过。