How to create the confirm box in mvc controller?

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 Dialog requires jquery.js, jquery-ui.js, jquery.ui.dialog.js scripts referenced in your page.

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

You can do this with 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.


Yes, you can do this with @Html.ActionLink as AliRıza Adıyahşi has commented.

Subscribe to the onclick event of the @Html.ActionLink

Here is the implementation:

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

And in javascript write the confirm box.

<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 isTrue querystring

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