List is being filled in but the properties are null

Asked

Viewed 58 times

1

Hello, I am 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, through 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 just can’t understand why the last parameter ( List pItens ) of the save method comes with the amount of items, but its properties are coming null.

This is the class :

    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; }
}

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&...

That is, You have to fill all properties explicitly for each element of the array,

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

Source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.