Current Field Date @ Html.EditorFor () Asp.Net MVC

1

Friend I want the field to come to the default already filled in without the user having to put the date

Howtoputcurrentdateinthefieldbydefault

@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 link solution below:

PROBLEM SOLVED WITH THE TUTORIAL OF THIS LINK

    
asked by anonymous 12.03.2018 / 02:38

2 answers

0

You just need to load the date into the model so you can switch 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);
        }
    
12.03.2018 / 14:48
0

The best place for you to fix is in your model builder.

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 at a discrepancy with your model and your view. Your view accesses the DataAbate field = > @Html.EditorFor(model => model.DataAbate, and your template does not have this field, please review this.

Another thing that should be noted is that yours returns you from your view on your first create is not a typed view. Change to return with the correct type.

That is

 Sequencia sequencia = new Sequencia();
 return View(sequencia);
    
12.03.2018 / 13:59