Complex type is getting null in a ApiController parameter

前端 未结 4 1240
借酒劲吻你
借酒劲吻你 2020-11-27 03:29

I don´t know why my parameter \"ParametroFiltro Filtro\" is getting null, the other parameters \"page\" and \"pageSize\" is getting OK.

public class Parametr         


        
4条回答
  •  天涯浪人
    2020-11-27 04:33

    You are trying to send a complex object with GET method. The reason this is failing is that GET method can't have a body and all the values are being encoded into the URL. You can make this work by using [FromUri], but first you need to change your client side code:

    $.ajax({
        url: fullUrl,
        type: 'GET',
        dataType: 'json',
        data: { Codigo: '_1', Descricao: 'TESTE', page: 1, pageSize: 10 },
        success: function (result) {
            alert(result.Data.length);
            self.Parametros(result.Data);
        }
    });
    

    This way [FromUri] will be able to pick up your complex object properties directly from the URL if you change your action method like this:

    public PagedDataModel Get([FromUri]ParametroFiltro Filtro, int page, int pageSize)
    

    Your previous approach would rather work with POST method which can have a body (but you would still need to use JSON.stringify() to format body as JSON).

提交回复
热议问题