Tuesday 10 October 2017

Server Side Yes No Confirmation Message Box in Controller in ASP.Net MVC Razor

Server Side Yes No Confirmation Message Box in Controller in ASP.Net MVC Razor

Description:


In this example we explain that how to create confirmation Box in Controller at Server Side in asp.net MVC Razor. Or Server Side Yes No Confirmation Box in Controller in MVC Razor View. How to create JavaScript confirmation box in controller’s action method in MVC Razor. Or how get the value of the confirmation box in controller’s action method in MVC Razor. Or how to pass the confirmation value from Razor view to controller’s action method in MVC.

Controller:

public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        // POST: Home
        [HttpPost]
        public ActionResult Index(string confirm_value)
        {
            if (confirm_value == "Yes")
            {
                ViewBag.Message = "You have clicked YES in Confirmation Box!";
            }
            else
            {
                ViewBag.Message = "You have clicked No in Confirmation Box!";
            }

            return View();
        }
    }

View:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Server Side Yes No Confirmation Message Box in Controller in ASP.Net MVC Razor</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Employee", 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>


1 comments:

  1. Copied Content from aspsnippets.com.
    Remove all copied content from aspsnippets.com in one week else copyright violation action will be taken against you and your blog.

    ReplyDelete