Current Date in @Html.Editorfor() Asp.Net MVC

Asked

Viewed 925 times

1

Friend want the field come to standard already filled without the need of the user to put the date

inserir a descrição da imagem aqui

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:

PROBLEM SOLVED WITH THIS LINK’S TUTORIAL

  • within the control you can pass the Datetime.now(), if you doubt post your controller I can write a solution

  • @Hudsonph I just put the controller, so you can write the solution.

2 answers

0

The Best place for you to make the correction is in the constructor of your model.

public class Sequencia
{
    public Sequencia()
    {
       DataSequencia = DateTime.now();
    }

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

But there’s a discrepancy with your model and your view. Your view accesses the field Dating => @Html.EditorFor(model => model.DataAbate, and your model doesn’t have that field, review that.

Another thing to note is that your return from your view in your first create is not a typed view. Change to return with the correct type.

That is to say

 Sequencia sequencia = new Sequencia();
 return View(sequencia);
  • All right this that you posted is forever save with the current date, but what I want is when I enter View Create or Edit the field already comes filled with the Current Date without the need of the user to fill and this does not happen, the field always comes worthless.

  • Have you ever been saved in the bank ? when you do your db.Sequencias.Find(id); it comes with your date field with a date?

  • even saving the date and when I go to the edit screen the date field does not come back with the value filled in, always have to put the date again.

  • I did what you asked me just above, I created the constructor I typed my view Create to return the date in the field, but nothing happens, in the constructor put to initialize other fields and in the others are filled.

0

You only need to load the date into the model to be able to view sequencia.DataSequencia = DateTime.now();

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);                    
            }
            Sequencia sequencia = db.Sequencias.Find(id);
            if (sequencia == null)
            {
                return HttpNotFound();
            }
            // aqui
            sequencia.DataSequencia = DateTime.now();
            return View(sequencia);
        }

Browser other questions tagged

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