Save the date and time?

0

I wanted to know how to save the date and time in the database, and the time wanted to be automatically pulled from the machine or server. So I was able to log the date, but the time is cleared.

MyViewModel:

publicintId{get;set;}publicdecimalCombustivel{get;set;}publicDateTimeDataSaida{get;set;}publicintAutorId{get;set;}publicintNumCarroId{get;set;}publicintKm{get;set;}

MyClassModel:

publicclassRota{publicvirtualintId{get;set;}publicvirtualDateTimeDataSaida{get;set;}publicvirtualdecimalCombustivel{get;set;}publicvirtualintKm{get;set;}publicvirtualUsuarioAutor{get;set;}publicvirtualVeiculoNumCarro{get;set;}}

DAOclass(Dotheinsertions):

publicvoidAdiciona(Rotarota){ITransactiontx=session.BeginTransaction();session.Save(rota);tx.Commit();}

Controllerclass:

publicActionResultAdiciona(RotaModelviewModel){varrotas=ckm.Consulta(viewModel.NumCarroId);//AquibuscatodasasrotasdesteveículovarmaiorRota=rotas.OrderByDescending(r=>r.Km).FirstOrDefault();//Aquivocêtemaúltimarotacadastrada,considerandoaregrageralif(maiorRota!=null&&viewModel.Km<maiorRota.Km)//Aquisenãotivervalorparafazercomparação(maiorRota!=null),eleiraregistrar.//Eleirafazeracomparaçãoeirasalvarseestiverdeacordo(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:

    
asked by anonymous 05.09.2017 / 22:35

2 answers

1

You create an object of type Rota soon after checking the validity of ModelState . You can set the date property there.

Rota rota = viewModel.CriaRota(); 
rota.DataSaida = DateTime.Now; 
dao.Adiciona(rota);
    
06.09.2017 / 16:47
0

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

Campo       Tipo         Default
datasaida   timestamp   current_timestamp

That way the bank itself takes the date for you.

Test there and give a feed. Hugs

    
05.09.2017 / 22:56