Field only records the date and the part of the time is "zeroed"

0

I have this piece of code in my view.

<div class="form-group">
            @*@Html.LabelFor(model => model.DT_AGENDAMENTO, htmlAttributes: new { @class = "control-label col-md-2" })*@
            @Html.Label("Data de Agendamento", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DT_AGENDAMENTO, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.DT_AGENDAMENTO, "", new { @class = "text-danger" })
            </div>
        </div>

And I have this code in my controller:

public async Task<ActionResult> Create([Bind(Include = "ID_SOLIC_RELATORIO,ID_RELATORIO,ID_USUARIO,DT_SOLICITACAO,DT_AGENDAMENTO,DT_GERACAO,BL_RELATORIO")] POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO)
        { 
            if (ModelState.IsValid)
            {
                db.POC_SOLIC_RELATORIO.Add(pOC_SOLIC_RELATORIO);
                //pOC_SOLIC_RELATORIO.BL_RELATORIO = AbrirExecutavelExtrairPdf();
                pOC_SOLIC_RELATORIO.DT_SOLICITACAO = DateTime.Now;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.ID_RELATORIO = new SelectList(db.POC_RELATORIO, "ID_RELATORIO", "NM_RELATORIO", pOC_SOLIC_RELATORIO.ID_RELATORIO);
            return View(pOC_SOLIC_RELATORIO);
        }

Using chrome dev tools, I inspected this field and had this:

<input class="form-control text-box single-line" data-val="true" data-val-date="The field DT_AGENDAMENTO must be a date." data-val-required="O campo DT_AGENDAMENTO é obrigatório." id="DT_AGENDAMENTO" name="DT_AGENDAMENTO" type="datetime" value="">

The problem is that when I write to the database the date from this textbox , it only writes the part of the date and the time is "zeroed". How do I save Date and Time?

EDIT Below is the complete Controller code.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Relatorio.Models;
using System.IO;
using System.Diagnostics;

namespace Relatorio.Controllers
{
    public class AppealReportController : Controller
    {
        private ReportDBContext db = new ReportDBContext();
        private byte[] PdfFile = null;
        ModelFiles oModelFiles = new ModelFiles();

        // GET: AppealReport
        public async Task<ActionResult> Index()
        {
            var pOC_SOLIC_RELATORIO = db.POC_SOLIC_RELATORIO.Include(p => p.POC_RELATORIO);
            return View(await pOC_SOLIC_RELATORIO.ToListAsync());
        }

        // GET: AppealReport/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO = await db.POC_SOLIC_RELATORIO.FindAsync(id);
            if (pOC_SOLIC_RELATORIO == null)
            {
                return HttpNotFound();
            }
            return View(pOC_SOLIC_RELATORIO);
        }

        // GET: AppealReport/Create
        public ActionResult Create()
        {
            ViewBag.ID_RELATORIO = new SelectList(db.POC_RELATORIO, "ID_RELATORIO", "NM_RELATORIO");
            //var _arquivos = oModelFiles.GetFileReport();
            return View();
        }

        // POST: AppealReport/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "ID_SOLIC_RELATORIO,ID_RELATORIO,ID_USUARIO,DT_SOLICITACAO,DT_AGENDAMENTO,DT_GERACAO,BL_RELATORIO")] POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO)
        { 
            if (ModelState.IsValid)
            {
                db.POC_SOLIC_RELATORIO.Add(pOC_SOLIC_RELATORIO);
                //pOC_SOLIC_RELATORIO.BL_RELATORIO = AbrirExecutavelExtrairPdf();
                pOC_SOLIC_RELATORIO.DT_SOLICITACAO = DateTime.Now;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.ID_RELATORIO = new SelectList(db.POC_RELATORIO, "ID_RELATORIO", "NM_RELATORIO", pOC_SOLIC_RELATORIO.ID_RELATORIO);
            return View(pOC_SOLIC_RELATORIO);
        }

        // GET: AppealReport/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO = await db.POC_SOLIC_RELATORIO.FindAsync(id);
            if (pOC_SOLIC_RELATORIO == null)
            {
                return HttpNotFound();
            }
            ViewBag.ID_RELATORIO = new SelectList(db.POC_RELATORIO, "ID_RELATORIO", "NM_RELATORIO", pOC_SOLIC_RELATORIO.ID_RELATORIO);
            return View(pOC_SOLIC_RELATORIO);
        }

        // POST: AppealReport/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "ID_SOLIC_RELATORIO,ID_RELATORIO,ID_USUARIO,DT_SOLICITACAO,DT_AGENDAMENTO,DT_GERACAO,BL_RELATORIO")] POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO)
        {
            if (ModelState.IsValid)
            {
                db.Entry(pOC_SOLIC_RELATORIO).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.ID_RELATORIO = new SelectList(db.POC_RELATORIO, "ID_RELATORIO", "NM_RELATORIO", pOC_SOLIC_RELATORIO.ID_RELATORIO);
            return View(pOC_SOLIC_RELATORIO);
        }

        // GET: AppealReport/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO = await db.POC_SOLIC_RELATORIO.FindAsync(id);
            if (pOC_SOLIC_RELATORIO == null)
            {
                return HttpNotFound();
            }
            return View(pOC_SOLIC_RELATORIO);
        }

        // POST: AppealReport/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO = await db.POC_SOLIC_RELATORIO.FindAsync(id);
            db.POC_SOLIC_RELATORIO.Remove(pOC_SOLIC_RELATORIO);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        //public void openApplication()
        //{
        //    System.Diagnostics.Process.Start("C:\Projetos\Servicos\bin\Servicos.exe");
        //}

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        protected static byte[] AbrirExecutavelExtrairPdf()
        {
            var proc = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = @"C:\Projetos\Servicos\bin\Debug\Servicos.exe",
                    Arguments = "",
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    RedirectStandardInput = true,
                    WorkingDirectory = @"C:\Projetos\Servicos\bin\Debug",
                    CreateNoWindow = true
                }
            };
            proc.Start();

            using (var ms = new MemoryStream())
            {
                using (var sOut = proc.StandardOutput.BaseStream)
                {
                    byte[] buffer = new byte[4096];
                    int read;

                    while ((read = sOut.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ms.Write(buffer, 0, read);
                    }
                }

                string error = proc.StandardError.ReadToEnd();

                if (ms.Length == 0)
                {
                    throw new Exception(error);
                }

                proc.WaitForExit();

                return ms.ToArray();
            }
        }

        public FileResult Download(int ID_SOLIC_RELATORIO, string NM_RELATORIO)
        {
            int _arquivoId = ID_SOLIC_RELATORIO;
            var arquivos = oModelFiles.GetFileReport(ID_SOLIC_RELATORIO, NM_RELATORIO);

            //string nomeArquivo = (from arquivo in arquivos
            //                      where arquivo. == _arquivoId
            //                      select arquivo.arquivoCaminho).First();

            //string nomeArquivo = (from arquivo in db.POC_SOLIC_RELATORIO
            //                      where arquivo.ID_SOLIC_RELATORIO == ID_SOLIC_RELATORIO
            //                      select arquivo.BL_RELATORIO).First().ToString();

            string contentType = "application/pdf";
            //Os parametros para o arquivo são
            //1. o caminho do aruivo on servidor
            //2. o tipo de conteudo do tipo MIME
            //3. o parametro para o arquivos salvo pelo navegador
            return File(arquivos, contentType, "novoreport.pdf");
        }
    }
}
    
asked by anonymous 11.11.2015 / 13:25

2 answers

3
  

I'm assigning you're using DateTime for dates.

I do not see anything wrong with your code, I believe you are not passing the values to time on your input .

I think you're filling in this way:

Whenshouldyoufillinthisway:

    
11.11.2015 / 14:43
-1

You create a database and put a date field and a time field and you put the type of it varchar.

And in php you put the following code. And then you create a database date and a contacts table.

<?php
$host = "localhost";
$user = "root";
$pass = "";
$banco = "data";

$conexao = mysql_connect($host,$user,$pass,$banco);

   $data = date("Y-m-d H:i");
   $hora = date('H:i');
   $query = "insert into contatos (data, hora) values ('{$data}', '{$hora}')";
   mysqli_query($conexao, $query);

                mysqli_close($conexao);
    
11.11.2015 / 13:58