0
I wanted to know how to save the date and time in the bank, and the time I wanted to pull automatically from the machine or server. So I was able to record the date, but the time is zero.
Man ViewModel
:
public int Id { get; set; }
public decimal Combustivel { get; set; }
public DateTime DataSaida { get; set; }
public int AutorId { get; set; }
public int NumCarroId { get; set; }
public int Km { get; set; }
My Class Model
:
public class Rota
{
public virtual int Id { get; set; }
public virtual DateTime DataSaida { get; set; }
public virtual decimal Combustivel { get; set; }
public virtual int Km { get; set; }
public virtual Usuario Autor { get; set; }
public virtual Veiculo NumCarro { get; set; }
}
DAO class(Makes the inserts):
public void Adiciona(Rota rota)
{
ITransaction tx = session.BeginTransaction();
session.Save(rota);
tx.Commit();
}
Controller class:
public ActionResult Adiciona(RotaModel viewModel)
{
var rotas = ckm.Consulta(viewModel.NumCarroId);
// Aqui busca todas as rotas deste veículo
var maiorRota = rotas.OrderByDescending(r => r.Km).FirstOrDefault();
// Aqui você tem a última rota cadastrada, considerando a regra geral
if (maiorRota != null && viewModel.Km < maiorRota.Km)
// Aqui se não tiver valor para fazer comparação (maiorRota != null), ele ira registrar.
// Ele ira fazer a comparação e ira salvar se estiver de acordo(viewModel.Km < maiorRota.Km).
{
ModelState.AddModelError("Km_Atual.Invalido",
"A quilometragem precisa ser maior que a anterior");
}
if (ModelState.IsValid)
{
Rota rota = viewModel.CriaRota();
dao.Adiciona(rota);
//return View();
return RedirectToAction("Index");
}
else
{
ViewBag.Usuarios = usuarioDAO.Lista();
ViewBag.Veiculo = veiculoDAO.Lista();
return View("Form", viewModel);
}
}
Tried to assign Datetime.Now to the property of your object ?
– Julio Soares
No, actually I don’t know where to put this Datetime.Now, I don’t know if it’s time to register at the bank or when to create the bank
– Guilherme Padovam
Put the code that makes the insertion in the database. Tell tb which is the database you use.
– Julio Soares
On the controller, do ->
Rota rota = viewModel.CriaRota(); rota.DataSaida = DateTime.Now; dao.Adiciona(rota);
– Jéf Bueno
I did not understand if it is the date and time or only the current time of the system, IE, the user informs some date and the system puts the current time, or ??? good I was in doubt.
– novic
It would only be the time, but if you take the current date and the time there is no problem
– Guilherme Padovam
@LINQ It worked out the method you explained, could by as response to I give the vote
– Guilherme Padovam
@I’ll do it now
– Jéf Bueno
@Guillermo Ready xD
– Jéf Bueno
Thank you @LINQ again for the help you have given me, and the others
– Guilherme Padovam