Save date and time?

Asked

Viewed 544 times

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.

inserir a descrição da imagem aqui

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);
            }
    }

ROUTE TABLE: inserir a descrição da imagem aqui

  • Tried to assign Datetime.Now to the property of your object ?

  • 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

  • Put the code that makes the insertion in the database. Tell tb which is the database you use.

  • On the controller, do -> Rota rota = viewModel.CriaRota(); rota.DataSaida = DateTime.Now; dao.Adiciona(rota);

  • 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.

  • It would only be the time, but if you take the current date and the time there is no problem

  • @LINQ It worked out the method you explained, could by as response to I give the vote

  • @I’ll do it now

  • @Guillermo Ready xD

  • Thank you @LINQ again for the help you have given me, and the others

Show 5 more comments

2 answers

1


You create an object of the type Rota after verifying the validity of the ModelState. It is possible set the date property there.

Rota rota = viewModel.CriaRota(); 
rota.DataSaida = DateTime.Now; 
dao.Adiciona(rota);

0

Vinicius. You can set this up right in the bank, put it like this:

Campo       Tipo         Default
datasaida   timestamp   current_timestamp

That way the bank itself gets the date for you.

Try it on and feed it. Hug

  • Catch the time too?

  • Yes, it does. It always works with me. I try to divide some tasks for the bank and the date and time is one of them :)

Browser other questions tagged

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