List is being populated but the properties are null

1

Hello, I'm using the following method in C #:

        public ActionResult Gravar(int pCodigo, string pDestino, int pRespDestino, int pOs, int pProjeto, string pEstabelecimento, string pObs, int pPim, string pAlmoxarifado, List<RequisicaoItem> pItens)
    {
        var elaborador = new intranetRepository.Adm.Repository.UsuarioRepository().CarregaUsuarioCompletoPorCodigo(new Usuario() { Id = new MVCUtilities().ObtemCodigoUsuario().GetValueOrDefault() });            

        var requisicao = new Requisicao()
        {
            Codigo = pCodigo,
            Destino = new CentroCusto() { Id = pDestino},
            RespDestino = new Usuario() { Id = pRespDestino },
            Uf = new CentroCusto() { Id = elaborador.Funcionario.CentroCusto.Id },
            RespUf = new intranetRepository.Adm.Repository.UsuarioRepository().CarregaUsuarioPorLogin(elaborador.Funcionario.Secao.Responsavel.UsuarioIntranet), 
            Os = pOs,
            Elaborador = new Usuario() { Id = elaborador.Id, Nome = elaborador.Nome },
            Projeto = new intranetRepository.Projeto.Model.Projeto() { Id = pProjeto} ,
            Estabelecimento = new Estabelecimento() { Sigla = pEstabelecimento},
            Observacao = pObs,
            Pim = new intranetRepository.Pim.Model.Pim() { Id = pPim },
            Almoxarifado = new Almoxarifado() { Sigla = pAlmoxarifado },
            Sistema = new Sistema() { Id = 2 },
            Itens = null
        };
        var retorno = new RequisicaoRepository().Gravar(requisicao);
        return Json(retorno, JsonRequestBehavior.AllowGet);
    }

This method, which is in the controller, is called by Javascript, using the following code:

$('#btnGravar').on('click', function () {

var itens = [];    
$('#divRequisicaoItem table tbody tr').each(function () {

    var item = {

        Localizacao : 'DGT222'
    };
    itens.push(item);
});

var param = {

    pCodigo: "0",
    pDestino: $("#ddlDestino").val(),
    pRespDestino: $("#ddlResponsavel").val(),
    pOs: "0",
    pProjeto: $("#txbProjetoCodigo").val() === '' ? "0" : $("txbProjetoCodigo").val(),
    pEstabelecimento: $("#ddlEstabelecimento").val(),
    pObs: $("#txbObservacao").val(),
    pPim: $("#txbPimCodigo").val() === '' ? "0" : $("#txbPimCodigo").val(),
    pAlmoxarifado: $("#ddlRetirarNo").val(),
    pItens: itens        
};

if ($('#formRequisicao').valid()) {

    if ($('#divRequisicaoItem table tbody tr').length >= 1) {

        $.ajax({

            url: '/Req/Requisicao/Gravar',
            data: param,
            beforeSend: function () {

                adicionarLoadingTela();
            }
        }).done(function (result) {


        });

    } else {
        var mensagem = {
            Tipo: 'A',
            Mensagem: 'Você precisa inserir pelo menos um item na requisição.'
        };
        exibirMensagem(mensagem, 100);
    }
}

});

I can not understand why the last parameter (List pItens) of the write method comes with the number of items, but its properties are coming null.

The class is this:

    public class RequisicaoItem
{
    public Item Item { get; set; }
    public Ordem Ordem { get; set; }
    public Deposito Deposito { get; set; }
    public string Localizacao { get; set; }
    public decimal Qtde { get; set; }
    public string DescricaoComplementar { get; set; }
    public decimal SaldoUju { get; set; }
    public decimal SaldoUsi { get; set; }
    public decimal SaldoUfl { get; set; }
    public decimal SaldoUfa { get; set; }
    public decimal SaldoUcb { get; set; }
}
    
asked by anonymous 06.07.2017 / 21:40

1 answer

1

The binding of an array / list of elements through the query string has to follow a special rule. In your case the parameter is called pItens .

Your query string must have the following format:

pItens[0].SaldoUju=valor&pItens[0].SaldoUsi=valor&...&  
pItens[1].SaldoUju=valor&pItens[1].SaldoUsi=valor&...

In other words, you have to fill in all the properties explicitly for each element of the array,

The best thing is to change your method to accept an http request with the Post method and receive the values in the format application\json in the body of the request. Binding will be done automatically and clients do not have to build a gigantic query string with a complicated format.

Font

    
07.07.2017 / 00:06