1
I need to do something like this feature: Jquery Autocomplete
jQuery:
$("#NumeroContrato").autocomplete({
source: function (request, response) {
$.ajax({
type: 'GET',
url: 'Cliente/ListarClientePorContratoJSON',
dataType: 'jsonp',
crossDomain: true,
success: function (data) {
response($.map(data, function (item) {
return {
label: item.Nome,
id: item.ClienteID
}
}))
},
});
},
minLength: 1,
select: function (event, ui) {
$("#NumeroContrato").val(ui.item.label);
$("#ClienteID").val(ui.item.id);
alert(ui.item.id);
event.preventDefault();
}
});
HTML:
<input type="hidden" id="ClienteID" name="ClienteID">
<div class="col-xs-2">
<label>Num. Contrato</label><br />
<input type="text" id="NumeroContrato" name="NumeroContrato" value="" class="form-control" />
</div>
Controller:
public JsonResult ListarClientePorContratoJSON(string NumeroContrato)
{
try
{
//AQUI VOU FAZER A CHAMADA PARA PESQUISA DO NUMERO DO CONTRATO:
}
catch (Exception)
{
return Json("erro", JsonRequestBehavior.AllowGet);
}
}
Problem:
The problem is that the variable Numerocontrato is always equal to Null
The solution was to change the parameter
crossDomain = true
, because it is no different domain, and I ended up changing also thetype: GET
forPOST
then it worked.– hard123
About the
crossDomain
that’s right. I’ve updated the answer. In case you usePOST
orGET
, works both ways. It all depends on how you set up your Controller.– Leonel Sanches da Silva