维护“在MVC中加载”页面上的复选框状态

时间:2019-07-10 10:35:55

标签: javascript asp.net model-view-controller

我的问题与页面加载有关。 我有以下代码:

控制器:

from django.views.static import serve

url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),

我有表public ActionResult Index() { BD.isEmailON_OFF= db.Email_ON_OFF_T.Select(x=>x.isEmailON_OFF).FirstOrDefault().GetValueOrDefault(); } [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 { }; }

我将isemailonoff列插入为0

功能:

我打开了电子邮件按钮和复选框。

打开电子邮件----->变为关闭按钮并选中复选框。

关闭按钮---->变为打开按钮,并且未选中复选框。

正常工作到这里。

打开电子邮件----->变为关闭按钮并选中复选框。

页面加载(正在加载页面)

IS_Email_on_OFF_T

单击关闭按钮(第二个),它会发生变化。

我尝试过的是:

观看次数:

clicking on turn off button and not changing to turn on the button(1st attempt)(ISSUE)

点击AJAX按钮

@{
                if (Model.isEmailON_OFF == 0)
                {
                    <input type="button" value="Turn Email on" class="btn btn-success" id="btnturnemailonoff" />
                            <input type="checkbox" id="Chkemailonoff" style="float:right;" />
                }
                else
                {
                    <input type="button" value="Turn Email off" class="btn btn-success" id="btnturnemailonoff" />
                            <input type="checkbox" id="Chkemailonoff" style="float:right;" checked/>
                }
              }

SP:

<script type="text/javascript">



    $(document).ready(function () {
               $('#btnturnemailonoff').on('click', function () {    


            var checked = !$(this).data('checked');
            var message = checked ? 'Turn Email ON' : 'Turn Email OFF';
            if (confirm("Do you want to " + message + "? ")) {



                $("#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");
                    }
                });
            }//end of if

        });
    });





</script>

1 个答案:

答案 0 :(得分:1)

我对AJAX呼叫按钮的单击进行了更改,它对我有用。

$(document).ready(function () {
        $('#btnturnemailonoff').on('click', function () {                              
            debugger;
             var checked = $("#Chkemailonoff").prop('checked');
            var message = checked ? 'Turn Email On' : 'Turn Email Off';
            if (confirm("Do you want to " + message + "? ")) {
                $("#Chkemailonoff").prop('checked', !checked);
                $(this).val(message)
                var url = '@Url.Action("CheckEmail", "BillingDetails")';
                $.ajax({
                    url: url,
                    type: "POST",
                    data: { checkemails: !checked },
                    dataType: "json",
                    success: function () {
                        //alert("ajax request to server succeed");
                    }
                });
            }




        });
    });