How to call a javascript function that is in a separate (.js) file from cshtml?

0
Hello, I have a page where the data of a DropDownList is a result of selecting a DropDownList before it, I created a function with javascript and ajax to perform the filter, everything works when javascript is within cshtml , but when I split the function into a .js file, I can no longer call the action within controller .     Below is the piece of code where I want to call the function that is in the file javascript :

   <div class="col-sm-3 col-xs-12">
      <div class="form-group">
         @Html.DefaultLabelFor(model => model.CompanhiaId)
         @Html.EditorFor(model => model.CompanhiaId, new { Items = Model.CompanhiaList , @onchange = "diretoria(1,'listarByCompany','skill')" } )
         @Html.ValidationMessageFor(model => model.CompanhiaId)
      </div>
   </div>

And here is the function that stays in the file javascript :

    $.ajax({
        type: "POST",
        url: "@Url.Action('listarByCompany','skill')",
        data: {
            id: id
        },
        success: function ajaxSucceess(response) {

            var dir = $("#DiretoriaExecutivaId").empty(); //Removendo todos os itens
            dir.append($("<option>Selecione...</option>"));

            $.each(response, function (i, response) {

                dir.append($('<option>', {
                    value: response.Value,
                    text: response.Text
                }));
            });
        }
    });

Does anyone know how to do this?

    
asked by anonymous 15.10.2018 / 18:30

1 answer

0

Personal thank you for the attention and help, I was able to solve the problem as follows:

<script src="@Url.Content("~/Scripts/diretoria.js")" type="text/javascript"></script>

Following the comment from Victor Laio in the first line (thanks Victor), I took the call from the htmlhelper:

Before:

@Html.EditorFor(model => model.CompanhiaId, new { Items = Model.CompanhiaList , @onchange = "diretoria(1,'listarByCompany','skill')" } )

Then:

@Html.EditorFor(model => model.CompanhiaId, new { Items = Model.CompanhiaList } )

I added the code below in cshtml to call the javascript function in the DropDownList change event:

<script> $("#CompanhiaId").change(function () { diretoria($("#CompanhiaId").find(":selected").val(), null, "listarByCompany", "skill", "DiretoriaExecutivaId"); }); </script>

    
16.10.2018 / 20:41