chetan kambli
chetan kambli

Reputation: 814

How to check and uncheck checkbox based on a button click (with text change of button) in Mvc?

I have button: Turn Email On and checkbox (which is unchecked) on side of that button.

When I click Turn email on button,it should call action method of controller where I have to write SP to update the email table.

Email_ON_OFF_T table have one field: isemailon_off(int type). I inserted value 0 into it.

So, Turn email on button's text will become turn email off and checkbox becomes checked.

I have tried the following code:

    <div class="col-md-8 form-group" style="text-align:right;">

                    <input type="button" value="Turn Email ON" class="btn btn-success" id="btnturnemailonoff" />
                </div>
                <div class="col-md-2 form-group">
                    <input type="checkbox" id="Chkemailonoff" />
                </div>

Ajax code to call button:

    $(document).ready(function () {
                    $('#btnturnemailonoff').on('click', function () {
                        //var prices = [];
                        //$('#Chkemailonoff').each(function () {

                        //    prices.push($(this).attr("value"));
                        //});
                        var checked = !$(this).data('checked');
                        $("#Chkemailonoff").prop('checked', checked);
                        $(this).val(checked ? 'Turn Email Off' : 'Turn Email on')
                        $(this).data('checked', checked);
                        debugger;
                        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");
                            }
                        });
                    });
                });

From Ajax, I am passing data(view) to the Controllers action Method.

    [HttpPost]
            public ActionResult CheckEmail(string checkemails)
            {

                //how to call Stored Procedure
                return new JsonResult { };
            }

I have written the below SP:

    ALTER procedure [dbo].[Sp_Email_on_off]
    @checkemails varchar(10)
    As
    Begin
    if(@checkemails='false')
    Update Email_ON_OFF_T set isEmailON_OFF=1 
    else
    Update Email_ON_OFF_T set isEmailON_OFF=0 

    End

The thing where I am stuck is the stored procedure. How to write and execute Sp in Controllers action Method? This is my first task in MVC. How to achieve this using Mvc?

Upvotes: 0

Views: 394

Answers (1)

Tomato32
Tomato32

Reputation: 2245

You can use FromSql or ExecuteSqlCommand to execute stored procedure.

FromSql can only be used to execute raw SQL queries or stored procedures to get the data. You can’t use it for INSERT/UPDATE/DELETE. If you want to execute INSERT, UPDATE, DELETE queries, use the ExecuteSqlCommand. It returns integer value which indicates the count of affected rows.

Here is a sample following your requirement. Hope to help, my friend :))

if(!string.IsNullOrWhiteSpace(checkemails))
{
    dataContext.Database
           .ExecuteSqlCommand("Sp_Email_on_off @checkemails", checkemails);
}

Upvotes: 2

Related Questions