0
Hello, I have a page where the data of a DropDownList are the result of the selection of a DropDownList previous to this, I created a function with javascript and ajax to realize the filter, everything works when the javascript is inside the cshtml, but when I separate the function into a file .js, I can no longer make the call to action within the 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 the function that is 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
}));
});
}
});
Can anyone tell me how to do this?
You are including the javascript file?
– Pbras
Yes Pbras, after doing the filter and returning the json in the action, I end up assembling the Dropdownlist with what was returned in the filter.
– Cyro Bergonzi
Where is the
function diretoria(){}– Leandro Angelo
It is inside the directory.js file in the web project’s script folder.
– Cyro Bergonzi
Place
<script src="@Url.Content("~/Scripts/SEU_SCRIPT.js")" type="text/javascript"></script>in your cshtml creating a proper . js for it, because putting yourself in the default layout will not have all dependencies yet when finished loading.– Victor Laio