In this article I will explain with an example, how to create Server Side Yes No JavaScript Confirmation Message Box in Controller’s Action method in ASP.Net MVC Razor.
When user will submit the Yes No JavaScript Confirmation Box in View the result i.e. Yes or No will be sent to the Controller’s Action method using Form submission. The result i.e. Yes or No can then be used to perform actions inside the Controller’s Action method in ASP.Net MVC Razor.
 
 
Controller
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
 
Action method for handling POST operation
This Action method gets called when the Form is submitted due to the click of the Submit button. The value of the dynamic HTML Hidden field sent from the View is captured inside the confirm_value variable.
Based on whether the User clicked Yes or No respective message is set into a ViewBag object.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
 
    // POST: Home
    [HttpPost]
    public ActionResult Index(string confirm_value)
    {
        if (confirm_value == "Yes")
        {
            ViewBag.Message = "You clicked YES!";
        }
        else
        {
            ViewBag.Message = "You clicked NO!";
        }
 
        return View();
    }
}
 
 
View
The following View consists of an HTML Form which posts to the Index action method discussed earlier. The Form consists of an HTML Submit button which when clicked, calls a JavaScript method Confirm before submitting the Form.
Inside the Confirm JavaScript method, a JavaScript Confirmation Message Box is displayed and the result is saved into a dynamic HTML Hidden Field which is later appended to the Form.
Finally the message returned from the Controller is displayed inside JavaScript Alert Message Box.
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        <input type="submit" value="Submit" onclick="Confirm()"/>
    }
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        function Confirm() {
            var input = $("<input />");
            input.attr("type", "hidden").attr("name", "confirm_value");
            if (confirm("Do you want to save data?")) {
                input.val("Yes");
            } else {
                input.val("No");
            }
            $("form")[0].appendChild(input[0]);
        }
    </script>
    @if (ViewBag.Message != null)
    {
        <script type="text/javascript">
            $(function () {
              alert("@ViewBag.Message")
            });
        </script>
    }
</body>
</html>
 
 
Screenshot
Server Side Yes No Confirmation Message Box in Controller in ASP.Net MVC Razor
 
 
Downloads