I have a controller called ProcessController along with its View, in this view I call a PartialView as Modal and in it I need to load data into a Jquery Select2 control.
PartialView
@model App.ViewModels.UnidadeOrganizacionalViewModel
@{
ViewData["Title"] = "Nova unidade organizacional";
}
<div class="modal-header">
<h4 class="modal-title">@ViewData["Title"]</h4>
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span><span class="sr-only">Fechar</span>
</button>
</div>
<form asp-action="CreateUnidadeOrganizacional">
<div class="modal-body">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="IdContratante" class="control-label"></label>
<select id="slcContratante" asp-for="IdContratante" class="form-control"></select>
<span asp-validation-for="IdContratante" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Nome" class="control-label"></label>
<input asp-for="Nome" class="form-control" />
<span asp-validation-for="Nome" class="text-danger"></span>
</div>
<div class="modal-footer" style="padding-right:0">
<input type="submit" value="Salvar" class="btn btn-success" />
<input type="button" class="btn btn-info" value="Fechar" data-dismiss="modal" />
</div>
</div>
</form>
<script src="~/lib/select2/js/select2.js"></script>
<script>
$(document).ready(function () {
$('#slcContratante').select2({
placeholder: 'Informe o contratante',
allowClear: true,
closeOnSelect: true,
tags: false,
minimumInputLength: 0,
language: {
inputTooShort: function () { return ""; },
noResults: function () {
return "Nenhum resultado encontrado";
},
searching: function () { return "Pesquisando..." }
},
ajax: {
type: 'GET',
url: '/get-contratante',
contentType: 'application/json',
dataType: 'json',
data: function (params) {
var query = {
search: params.term
}
return query;
},
processResults: function (json) {
return { results: objThat.MapSelect2(json) };
}
}
});
});
</script>
When I select Select2 it should open a dropdown with the data obtained through the ajax method that makes a call in ProcessController
[Route("get-contratante")]
public async Task<JsonResult> GetContratante(string search)
{
IEnumerable<ContratanteViewModel> lstContratanteViewModel = null;
lstContratanteViewModel = _mapper.Map<IEnumerable<ContratanteViewModel>>(await _contratanteBLL.GetByNome(search));
return new JsonResult(lstContratanteViewModel);
}
No Ajax request happens, I checked the console and it has no error message and the Select2 control is correct when called by JQuery.
I tested the same Select2 in ProcessController View and the data was loaded, but in PartialView nothing happens.
Where am i going wrong?