1
Friend want the field come to standard already filled without the need of the user to put the date
How to put current date in the default field
@Html.EditorFor(model => model.DataAbate, new { htmlAttributes = new { @class = "form-control" } })
Class
public class Sequencia
{
[Key]
public int SequenciaId { get; set; }
[Display(Name = "Data da Sequencia")]
[Required(ErrorMessage = "O Campo {0} é requirido!")]
[DataType(DataType.Date)]
public DateTime DataSequencia { get; set; }
}
Controller
using GerenciamentoDeQuartos.Classes;
using GerenciamentoDeQuartos.Models;
using System;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
namespace GerenciamentoDeQuartos.Controllers
{
public class SequenciasController : Controller
{
private GerenciamentoDeQuartosContext db = new GerenciamentoDeQuartosContext();
// GET: Sequencias
public ActionResult Index()
{
return View(db.Sequencias.ToList());
}
// GET: Sequencias/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Sequencia sequencia = db.Sequencias.Find(id);
if (sequencia == null)
{
return HttpNotFound();
}
return View(sequencia);
}
// GET: Sequencias/Create
public ActionResult Create()
{
ViewBag.LadoA = new SelectList(CombosHelper.GetTipoLado(), "TipoLadoId", "Nome");
ViewBag.CamaraLadoA = new SelectList(CombosHelper.GetCamara(), "CamaraId", "Nome");
ViewBag.LadoB = new SelectList(CombosHelper.GetTipoLado(), "TipoLadoId", "Nome");
ViewBag.CamaraLadoB = new SelectList(CombosHelper.GetCamara(), "CamaraId", "Nome");
return View();
}
// POST: Sequencias/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Sequencia sequencia)
{
if (ModelState.IsValid)
{
db.Sequencias.Add(sequencia);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(sequencia);
}
// GET: Sequencias/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Sequencia sequencia = db.Sequencias.Find(id);
if (sequencia == null)
{
return HttpNotFound();
}
ViewBag.LadoA = new SelectList(CombosHelper.GetTipoLado(), "TipoLadoId", "Nome", sequencia.LadoA);
ViewBag.CamaraLadoA = new SelectList(CombosHelper.GetCamara(), "CamaraId", "Nome", sequencia.CamaraLadoA);
ViewBag.LadoB = new SelectList(CombosHelper.GetTipoLado(), "TipoLadoId", "Nome", sequencia.LadoB);
ViewBag.CamaraLadoB = new SelectList(CombosHelper.GetCamara(), "CamaraId", "Nome", sequencia.CamaraLadoB);
return View(sequencia);
}
// POST: Sequencias/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Sequencia sequencia)
{
if (ModelState.IsValid)
{
db.Entry(sequencia).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(sequencia);
}
// GET: Sequencias/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Sequencia sequencia = db.Sequencias.Find(id);
if (sequencia == null)
{
return HttpNotFound();
}
return View(sequencia);
}
// POST: Sequencias/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Sequencia sequencia = db.Sequencias.Find(id);
db.Sequencias.Remove(sequencia);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
Problem solved with this solution from the link below:
within the control you can pass the Datetime.now(), if you doubt post your controller I can write a solution
– HudsonPH
@Hudsonph I just put the controller, so you can write the solution.
– Cyberlacs