Confirmation message box in controller in asp.net MVC using javascript

In this article, we will learn how to call confirmation message box from server side in MVC.Here we call the YES and NO method in confirmation message box using JavaScript.



Controller:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCCODESOLUTIONS.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string confirm_value)
        {
            if (confirm_value == "Yes")
            {
                ViewBag.Message = "Your value delete successfully";
            }
            else

            {
                ViewBag.Message = " You cancel the transactions";
            }

            return View();
        }
    }
}


View
@{
    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="Click Here For Confirmation Message" onclick="Confirmationfun()" />

    }
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        function Confirmationfun() {
            var input = $("<input />");
            input.attr("type", "hidden").attr("name", "confirm_value");
            if (confirm("Hi, Do you want delete the value?")) {
                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>



Out-Put:-


Confirmation message box in controller in asp.net MVC using javascript Confirmation message box in controller in asp.net MVC using javascript Reviewed by NEERAJ SRIVASTAVA on 10:39:00 AM Rating: 5

No comments:

Powered by Blogger.