Error when registering in the form

Asked

Viewed 56 times

0

When you select the value and I will register informs me an error.

System.Argumentexception: The value "System.Object[]" is not of type "Blogweb.Models.Compra" and cannot be used in this Generic Collection. Parameter name: value

Abastecimento Controller:

public ActionResult Adiciona(AbastecimentoModel viewModel)
{
    var Produto = ckm.ConsultaValor(viewModel.NomeProdutoId);

        //*var teste = ckm.ConsultaValor(compra.VlrUnit);
        var Valor = Produto.OrderByDescending(compra => compra.VlrUnit).Last();

        viewModel.VlrUnitId = Valor.VlrUnit;

    if (ModelState.IsValid)
    {
        Abastecimento abastecimento = viewModel.CriaAbastecimento();
        dao.Adiciona(abastecimento);
        //return View();
        return RedirectToAction("Index");
    }
    else
    {
        ViewBag.Compra = compraDAO.Lista();
        ViewBag.Usuarios = usuarioDAO.Lista();
        ViewBag.Veiculo = veiculoDAO.Lista();
        return View("Form", viewModel);
    }

}

Controlekm Clase:

public IList<Abastecimento> ConsultaProduto(int NomeProdutoId)
    {
        string hql = "SELECT a FROM Abastecimento a";
        IQuery query = session.CreateQuery(hql);
        return query.List<Abastecimento>();
    }
    public IList<Compra> ConsultaValor(float VlrUnit)
    {
        string hql = "SELECT * FROM Compra";
        IQuery query = session.CreateSQLQuery(hql);
        return query.List<Compra>();
    }

}

Abastecimentomodel:

    public Abastecimento CriaAbastecimento ()
    {
        Abastecimento abastecimento = new Abastecimento()
        {
            Id = this.Id,
            Km = this.Km,
            DtAbastecido = this.DtAbastecido,
            Litro = this.Litro,
            VlrUnit = this.VlrUnit,
            TotalGasto = this.TotalGasto
        };
        if (this.AutorId != 0)
        {
            Usuario autor = new Usuario()
            {
                Id = this.AutorId
            };
            abastecimento.Autor = autor;
        }
        if (this.NumCarroId != 0)
        {
            Veiculo numcarro = new Veiculo()
            {
                Id = this.NumCarroId
            };
            abastecimento.NumCarro = numcarro;
        }
        if (this.NomeProdutoId != 0)
        {
            Compra nomeproduto = new Compra()
            {
                Id = this.NomeProdutoId
            };
            abastecimento.NomeProduto = nomeproduto;
        }
        return abastecimento;
    }

1 answer

0

Somewhere in the code is setting Blogweb.Models.Supply the value 2.9, possibly on

viewModel.CriaAbastecimento()

Verify that Creatiprovisioning() is returning a valid value.

Browser other questions tagged

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