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