skip to Main Content

This Partial view:

@model SetIsApprovedModel

    <form id="frmApprove" data-ajax="true" data-ajax-method="post" data-ajax-url="@(Url.Action("SetApprove", "AccountAdmin"))"
          data-ajax-success="onsuccess" data-ajax-mode="replace" data-ajax-update="#approvePartial" data-ajax-loading="#loadingApprove"
          data-ajax-failure="failed">
        <input type="hidden" asp-for="@Model.UserName" />
        <div class="row px-3 mb-1">
            <div class="col-md-5 text-md-end fw-bold">Approved: </div>
            <div class="col-md-7 text-md-start" id="IsApprovedDiv">
                <input id="chkApprove" type="checkbox" asp-for="@Model.IsApproved" />
            </div>
            <input type="submit" value="Submit" />
        </div>
    </form>
    
    <script>
        $(document).ready(function () {
            $(document).on('change', "input[type='checkbox']", function (e) {
                /*$("#chkApprove").on('change', function (evt) {*/
                $('#frmApprove').trigger('submit');
            });
        });
    </script>

Script is not working how to correct it?
Note: if I click submit button it works but I want this form is posted in response to change event from checkbox.

2

Answers


  1. You need to attach the handler to the checkbox, as you seem to have tried. Also, you can use the jQuery submit method.

    $("#chkApprove").on('change', function (evt) {
      $('#frmApprove’).submit();
    });
    
    Login or Signup to reply.
  2. I’m afraid the script written in the partial view won’t work after the partial view is rendered in a main view because it’s a cshtml file. So I have a test which move the script into the main view surrounded by @section Scripts{} and you can see my test result.

    enter image description here

    Or you can include jquery library in your partial view as well. For example if you have a default asp.net core MVC project, you can write <script src="~/lib/jquery/dist/jquery.min.js"></script> before your script.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search