Jquery does not send the parameter to search

1

I need to do something like this: 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 Numero Contratrato variable is always equal to Null

    
asked by anonymous 01.03.2016 / 14:02

1 answer

3

If it is GET , you need to put the parameter in the URL, otherwise it will not work, obviously:

$("#NumeroContrato").autocomplete({
    source: function (request, response) {
        $.ajax({
            type: 'GET',
            url: '/Cliente/ListarClientePorContratoJSON/' + $("#NumeroContrato").val(), // Não sei se $(this).val() funciona aqui. Vale a pena um teste. 
            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();
    }
});

If the request is made within the same site, you do not need to use crossDomain: true , nor dataType: jsonp .

    
01.03.2016 / 16:52