0

when i use this code for show modal in asp.net core5 ,get this Error:( Uncaught ReferenceError: ShowModalEdituser is not defined at HTMLButtonElement.onclick()).and my modal don't show how can solve this Error. this is my code.

<button type="button" class="btn btn-primary btn-lg"  onclick="ShowModalEdituser('@item.id', '@item.FullName')" >Edite</button>

@section Scripts

{

    function EditUser1() {
        var usrId = $("#Edite_userId").val();
        var fullname = $("#Edite_fullname").val();
        var postData = {
            'usrId': usrId,
            'fullname': fullname,
        };

        $.ajax({
            contentType: 'application/x-www-form-urlencoded',
            dataType: 'json',
            type: "POST",
            url: "Edite",
            data: postData,
            success: function (data) {
                if (data.issuccess == true) {
                    swal.fire(
                        'موفق!',
                        data.message,
                        'success'
                    ).then(function (isConfirm) {
                        location.reload();
                    });
                }
                else {
                    swal.fire(
                        'هشدار!',
                        data.message,
                        'warning'
                    );
                }
            },
            error: function (request, status, error) {
                alert(request.responseText);
            }
        });
        $('#Editeuser').Modal('hide');
    }

    function ShowModalEdituser(UserId, fullName) {
        $('#Edit_Fullname').val(fullName)
            $('#Edit_UserId').val(UserId)


            $('#EditUser').Modal('show')

    }

}

@section Modals { Edite User × fullname close set }

@section Modals { Edite User × fullname close set }

`

1 Answers1

0

When you want to call JS function in Asp.net core MVC, you need put them in <script></script> tag

@section Scripts{
  <script>
      function EditUser1() {
      var usrId = $("#Edite_userId").val();
      var fullname = $("#Edite_fullname").val();
      var postData = {
        'usrId': usrId,
        'fullname': fullname,
      };

    $.ajax({
        contentType: 'application/x-www-form-urlencoded',
        dataType: 'json',
        type: "POST",
        url: "Edite",
        data: postData,
        success: function (data) {
            if (data.issuccess == true) {
                swal.fire(
                    'موفق!',
                    data.message,
                    'success'
                ).then(function (isConfirm) {
                    location.reload();
                });
            }
            else {
                swal.fire(
                    'هشدار!',
                    data.message,
                    'warning'
                );
            }
        },
        error: function (request, status, error) {
            alert(request.responseText);
        }
    });
    $('#Editeuser').Modal('hide');
  }

  function ShowModalEdituser(UserId, fullName) {
    $('#Edit_Fullname').val(fullName)
        $('#Edit_UserId').val(UserId)


        $('#EditUser').Modal('show')

}
    
  </script>

}

You can refer to Script Tag Helper in ASP.NET Core.

enter image description here

Xinran Shen
  • 8,416
  • 2
  • 3
  • 12
  • tank's for your answer, but i used – Hossein Beygpour Jun 06 '22 at 17:52
  • And it still don't work? can you provide your whole view code? – Xinran Shen Jun 07 '22 at 00:50
  • yes ,Of course.you can see my code in github at https://github.com/hosseinbeygpour/uci – Hossein Beygpour Jun 07 '22 at 18:26
  • Hi @Hossein Beygpour, I check your github and find that you don't use the default layout page, I use the default loyout page to test. Then i just use your code and it works fine, The only problem i find that here need to `$('#EditUser').modal('show')`, If you use `Modal(xx)` it will report an error `$(...).Modal is not a function` – Xinran Shen Jun 08 '22 at 03:03
  • hi Xinran Shen dear. that 's right . but my problem solve via atach jquery.min.js and other related file to default layuot . tank you. but now i get this error when i click on button that there is in modal(اعمال تغییرات) . i get this erroe Uncaught ReferenceError: Edituser1 is not defined at HTMLAnchorElement.onclick – Hossein Beygpour Jun 08 '22 at 20:26
  • sorry bro, I can't get the same error as yours, But i think you can refer to this [issue](https://stackoverflow.com/questions/17378199/uncaught-referenceerror-function-is-not-defined-with-onclick), may be it can solve your problem – Xinran Shen Jun 09 '22 at 01:07