Execute code on Confirmation Box OK Button Click using JavaScript in ASP.Net MVC

Last Reply one month ago By dharmendr

Posted one month ago

I wanted to write condition for confirmation box such that : There is a button: Turn on email.

So when user click on it,it becomes turn off email and sends status as true.

when i again click on turn off email,it becomes turn on email and sends status as false.

I wanted to write condition for confirmation box such that :

when user click on turn on button,confirmation box should come like Do you want to turn on the email? if yes,it should change to turn off button and sends status to controller(true) and if no,it stays as it is.

It becomes turn off button(when we click on yes).So when i click aagin on turn off button,confirmation box should come like Do you want to turn off the email? if yes,it should change to turn on button and sends status to controller(false) and if no, it stays as it is.

            $(document).ready(function () {
                $('#btnturnemailonoff').on('click', function () {
                    var checked = !$(this).data('checked');
                    $("#Chkemailonoff").prop('checked', checked);
                    $(this).val(checked ? 'Turn Email Off' : 'Turn Email on')
                    $(this).data('checked', checked);

                    //if (confirm("Do you want to turn on the email?")) {
                    //    checked.val("true");
                    //} else {
                    //    checked.val("false");
                    //}
                    var url = '@Url.Action("CheckEmail", "BillingDetails")';
                    $.ajax({
                        url: url,
                        type: "POST",
                        data: { checkemails: checked },
                        dataType: "json",
                        // traditional: true,
                        success: function () {
                            alert("ajax request to server succeed");
                        }
                    });
                });
            });

 

        [HttpPost]
        public ActionResult CheckEmail(string checkemails)
        {           
            //calling Stored Procedure

            if (!string.IsNullOrWhiteSpace(checkemails)|| checkemails=="true" || checkemails=="false")
            {
                var checkemails1 = new SqlParameter("checkemails", checkemails);
                db.Database.ExecuteSqlCommand("EXEC Sp_Email_on_off @checkemails", checkemails1);
            }

            return new JsonResult { };
        }

 

You are viewing reply posted by: dharmendr one month ago.
Posted one month ago

Hi chetan,

Check this example. Now please take its reference and correct your code.

Controller

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult CheckEmail(string checkemails)
    {
        //calling Stored Procedure
        if (!string.IsNullOrWhiteSpace(checkemails) || checkemails == "true" || checkemails == "false")
        {
            var checkemails1 = new SqlParameter("checkemails", checkemails);
            db.Database.ExecuteSqlCommand("EXEC Sp_Email_on_off @checkemails", checkemails1);
        }
        return new JsonResult { };
    }
}

View

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('[id*=btnturnemailonoff]').on('click', function () {
            var checked = !$(this).data('checked');
            var message = checked ? 'Turn Email Off' : 'Turn Email ON';
            if (confirm("Do you want to " + message + "? ")) {
                $("#Chkemailonoff").prop('checked', checked);
                $(this).val(checked ? 'Turn Email Off' : 'Turn Email ON');
                $(this).data('checked', checked);
                $.ajax({
                    url: "Home/CheckEmail",
                    type: "POST",
                    data: { checkemails: checked },
                    dataType: "json",
                    success: function () {
                        alert("ajax request to server succeed");
                    }
                });
            }
        });
    });
</script>
<input type="button" value="Turn Email ON" id="btnturnemailonoff" />
<input type="checkbox" id="Chkemailonoff" />

Screenshot