I need to have my two tables work together, where Abastecimento
takes the Product Name ( NomeProdutoId
), from the Compra
table, I was able to get it to get the Product Name, but wanted me to If you do not know the name of the product, you will have to enter the value of that product, and register in the Abastecimento
table, an example would be those registration forms, where you inform your state and just below you only bring cities from that state.
But in my case I wanted the value, and the last registered value, of that product.
This part was an attempt where in another form I 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 a foreign key, that's fine.
Model Buy:
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 Supply:
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>();
}