Error trying to register

Asked

Viewed 83 times

0

Good afternoon, the moment I try to register in the form, I program in the language c# with Asp.net, Razor and Nhibernate.

This is my DAO

public IList<Rota> Comparacao()
        {
        //*string hql = "Select p from Rota p";
        string hql = "SELECT Id, DtLancamento, Km_Atual, Combustivel, NVeiculoId, (SELECT TOP 1 x.Km_Atual FROM Rota x WHERE x.NVeiculoId  = r.NVeiculoId AND x.Id != r.Id AND x.DtLancamento < r.DtLancamento Order By x.DtLancamento desc, x.Id desc) as Km_Anterior FROM Rota r";
        IQuery query = session.CreateSQLQuery(hql);
        return query.List<Rota>();
        }

My Controller

dao.Comparacao();
            if (viewModel.Km_Atual < p.Km_Atual)
                    {
                    ModelState.AddModelError("Km_Atual.Invalido",
                    "Km Atual precisa ser maior que o atual");
                    }

                if (ModelState.IsValid)
                    {
                    Rota rota = viewModel.CriaRota();
                    dao.Adicionar(rota);
                    //return View();
                    return RedirectToAction("Form");
                    }
                else
                    {
                    ViewBag.Veiculo = veiculoDao.Lista();
                    return View("Index", viewModel);
                    }
                }

My Model

public class Rota
        {
        public virtual int Id { get; set; }
        public virtual DateTime DtLancamento { get; set; }
        public virtual int Km_Atual { get; set; }
        public virtual int Km_Anterior { get; set; }
        public virtual float Combustivel { get; set; }
        public virtual Veiculo NVeiculo { get; set; }
        }
    }

My Viewmodel

public class RotaModel
    {
    public int Id { get; set; }
    public DateTime DtLancamento { get; set; }
    public int Km_Atual { get; set; }
    public int Km_Anterior { get; set; }
    public float Combustivel { get; set; }
    public int NVeiculoId { get; set; }

    public Rota CriaRota()
        {
        Rota rota = new Rota()
        {
            Id = this.Id,
            DtLancamento = this.DtLancamento,
            Km_Atual = this.Km_Atual,
            Km_Anterior = this.Km_Anterior,
            Combustivel = this.Combustivel
        };

        if (this.NVeiculoId != 0)
        {
            Veiculo ncarro = new Veiculo()
        {
            Id = this.NVeiculoId
        };
            rota.NVeiculo = ncarro;
        }
        return rota;
        }

    public RotaModel(Rota p)
        {
        this.Id = p.Id;
        this.DtLancamento = p.DtLancamento;
        this.Km_Atual = p.Km_Atual;
        this.Km_Anterior = p.Km_Anterior;
        this.Combustivel = p.Combustivel;
        if (p.NVeiculo != null)
            {
            this.NVeiculoId = p.NVeiculo.Id;
            }
        }
    public RotaModel()
    {
    //* Construtor vazio, sempre necessario!! <-- Importante.    
    }
    }
}

The error that appears when I try to register is this: The value "System.Object[]" is not "Blogweb.Models.Rota" and cannot be used in this generic collection.

  • 1

    The mistake already says what the problem is.

  • I’m a beginner in c#, but how could I solve this problem?

  • Just to make sure, which line of error?

  • What is the type of viewModel in the controller?

  • It appears on the DAO’s Return line - "Return query.List<Route>();"

  • I put my viewmodel in the question to make it easier

  • I believe the solution is that you have to transform the value for the Rota class. TransformUsing(Transformers.AliasToBean<Rota>())

  • I put this in the line of the Return right?

Show 3 more comments

1 answer

0


Try turning the columns to the class Rota using the Transform of nhibernate, in this way:

IQuery query = session.CreateSQLQuery(hql).TransformUsing(Transformers.AliasToBean<Rota>());

Browser other questions tagged

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