1
I need to get my two tables to work together, where the Abastecimento
take the Product Name(NomeProdutoId
), table Compra
, I managed to get him to take the Product Name, but I wanted him to bring me the Value of that Product, and register it in the table Abastecimento
, an example would be those registration forms, where you inform your state and just below behind only cities of that state.
But in mine I wanted the value, and the last recorded value, of that product.
That part was an attempt where in another form used a scheme to bring me a vehicle and then compare the KM.
If you need to change the query and the controller I can, or put foreign key, no problem.
Model Compra:
public class Compra
{
public virtual int Id { get; set; }
public virtual string Tipo { get; set; }
public virtual float VlrUnit { get; set; }
public virtual float VlrTotal { get; set; }
public virtual int Quant { get; set; }
public virtual string NomeProduto { get; set; }
public virtual DateTime DtCompra { get; set; }
public virtual Fornecedores Nome { get; set; }
}
Model Abastecimento:
public class Abastecimento
{
public virtual int Id { get; set;}
[Required]
public virtual int Litro { get; set; }
public virtual DateTime? DtAbastecido { get; set; }
public virtual float VlrUnit { get; set; }
public virtual int Km { get; set; }
public virtual float TotalGasto { get; set; }
public virtual Usuario Autor { get; set; }
public virtual Compra NomeProduto { get; set; }
public virtual Veiculo NumCarro { get; set; }
}
Controller:
public ActionResult Adiciona(AbastecimentoModel viewModel)
{
var Produto = ckm.ConsultaProduto(viewModel.NomeProdutoId);
/*Aqui na verdade é um teste, mas o objetivo dele seria para pegar os valores que tem na tabela compra, e então utilizar na variavel Valor, para ordernar*/
var teste = ckm.ConsultaValor(compra.VlrUnit);
var Valor = Produto.OrderByDescending(a => teste).Last();
viewModel.TotalGasto = viewModel.Litro * viewModel.VlrUnitId;
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);
}
}
Query:
public IList<Compra> ConsultaValor(float VlrUnit)
{
string hql = "SELECT c FROM Compra c";
IQuery query = session.CreateQuery(hql);
return query.List<Compra>();
}
"the problem that returns value, zero or else value of another variable". Can you explain it a little better? It’s kind of confusing.
– Jéf Bueno
@Marconciliosouza I don’t understand so much of c# with nhibernate, but in another form, I was able to use this way, where the select took all the information and the controller could organize
– Guilherme Padovam
@LINQ do not know if improved, but it would be basically where I try to pull the value and it does not pull, so not registering anything
– Guilherme Padovam
Buddy, you still don’t know what you really need, or don’t know how to expose it here. Do not give to elaborate an answer without you put your classes , and say what you expect from your selects.
– Marco Souza
@Marconciliosouza I think it’s now clearer, and the models have been posted to shows where you have keys and which the key
– Guilherme Padovam
what is the relationship between the product table and purchase ? post the Product also
– Marco Souza
There is no table
Produto
, the product I say, is when you register in the tableCompra
, every product that existed and will exist is in the tableCompra
– Guilherme Padovam