Returns query value in a variable

Asked

Viewed 168 times

1

Precise returns the value of a query query in an int variable, to be able to make the comparison with my Textbox, but I cannot make the comparison because I cannot compare integer(textbox) with a group of methods(jquery), follow my code below.

Controlerota Class:

public IList<Rota> ConsultaCarro (int NVeiculoId)
{
    string hql = "SELECT * FROM Rota r WHERE r.NVeiculoId = :NVeiculoId";
    IQuery query = session.CreateQuery(hql);
    query.SetParameter("NVeiculoId", NVeiculoId);
    return query.UniqueResult<Rota>(); 
    return query.List<Rota>();
}

Controller:

public ActionResult Adicionar(RotaModel viewModel)
{
    if (/*Onde entraria o valor da query*/ = viewModel.NVeiculoId)
    {

    }

    if (viewModel.Km_Atual < 85000)
    {
        ModelState.AddModelError("Km_Atual.Invalido",
        "A quilometragem precisa ser maior que a anterior");
    }
    if (ModelState.IsValid)
    {
        Rota rota = viewModel.CriaRota();
        dao.Adicionar(rota);
        //return View();
        return RedirectToAction("Form");
    }
    else
    {
        ViewBag.Veiculo = veiculoDao.Lista();
        return View("Index", viewModel);
    }
} 

1 answer

1


From the comments, I think that’s what you need

public ActionResult Adicionar(RotaModel viewModel)
{
    var rotas = ConsultaCarro(viewModel.NVeiculoId);
    // Aqui busca todas as rotas deste veículo

    var maiorRota = rotas.OrderByDescending(r => r.Km_Atual).First();
    // Aqui você tem a última rota cadastrada, considerando a regra geral   

    if (viewModel.Km_Atual < maiorRota.Km_Atual)
    {
        ModelState.AddModelError("Km_Atual.Invalido",
        "A quilometragem precisa ser maior que a anterior");
    }

    if (ModelState.IsValid)
    {
        Rota rota = viewModel.CriaRota();
        dao.Adicionar(rota);
        //return View();
        return RedirectToAction("Form");
    }
    else
    {
        ViewBag.Veiculo = veiculoDao.Lista();
        return View("Index", viewModel);
    }
} 
  • Thank you LINQ, but only a doubt how he will get the Veiculoid?

  • But why seek? He is already in viewModel.NVeiculoId.

  • Then you will bring me only the value of that vehicle?

  • That. This is not the expected behavior???

  • No need to call in sql the veiculoid to bring only the values of that vehicle?

  • Yes, it’s just that I was imagining about three hundred lines of kkk code

  • But that’s what the parameter in the method is for ConsultaCarro

  • Got it, no problem if sql is string no?

  • No, Jow. It’s unrelated

  • I’ll test the code, I’ll let you know if there’s any mistakes, I pray there aren’t any

  • LINQ when was adding a km in a vehicle that has no value at all would have to do another add or if?

  • It worked, the code, only gave problem when it goes to add a km in a vehicle that has nothing registered

  • LINQ thank you very much, I function perfecting, I gave an adjusted that when it is registered the vehicle, it will always put 0 km, thank you very much.

Show 8 more comments

Browser other questions tagged

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