复杂类型越来越空的ApiController参数 [英] Complex type is getting null in a ApiController parameter

查看:132
本文介绍了复杂类型越来越空的ApiController参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我的参数ParametroFiltro Filtro越来越空,其他参数页和pageSize的越来越好。

 公共类ParametroFiltro
{
    公共字符串Codigo {获得;组; }
    公共字符串Descricao {获得;组; }
}
 

我的ApiController Get方法:

 公共PagedDataModel< ParametroDTO>获取(ParametroFiltro Filtro,诠释页,诠释的pageSize)
 

我的Ajax调用:

  VAR fullUrl =/ API /+ self.Api;
$阿贾克斯({
    网址:fullUrl,
    键入:GET,
    数据类型:JSON,
    数据:{Filtro:{Codigo:_1,Descricao:TESTE'},页面:1,每页:10},
    成功:函数(结果){
        警报(result.Data.length);
        self.Parametros(result.Data);
    }
});
 

解决方案

您要发送与 GET 方法的复杂对象。这种失败的原因是, GET 方法不能有身体,所有的值都被连接codeD插入URL。您可以使用 [FromUri] ,使这项工作,但是首先你需要改变你的客户端code:

  $。阿贾克斯({
    网址:fullUrl,
    键入:GET,
    数据类型:JSON,
    数据:{Codigo:_1,Descricao:TESTE,页面:1,每页:10},
    成功:函数(结果){
        警报(result.Data.length);
        self.Parametros(result.Data);
    }
});
 

这样 [FromUri] 将能够直接从URL拿起您的复杂对象的属性,如果你改变你的操作方法是这样的:

 公共PagedDataModel< ParametroDTO>获得([FromUri] ParametroFiltro Filtro,诠释页,诠释的pageSize)
 

您previous办法宁愿与岗位工作它可以有一个主体(方法,但你仍然需要使用 JSON.stringify ()来格式化身体JSON)。

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

public class ParametroFiltro
{
    public string Codigo { get; set; }
    public string Descricao { get; set; }
}

My ApiController Get method:

public PagedDataModel<ParametroDTO> Get(ParametroFiltro Filtro, int page, int pageSize)

My ajax call:

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

解决方案

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<ParametroDTO> 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).

这篇关于复杂类型越来越空的ApiController参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆