Dataannotations With Datetime

Asked

Viewed 235 times

0

I’d like some help. Following model:

    [Display(Name = "Data Aquisição")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = false)]
    public DateTime PatDtAquisicao { get; set;}

I’m having trouble using the above Annotation, the problem occurs when I send the model to my Edit View, debugging the code, the date value is sent correctly to the View, but the date field does not receive the value and is Default. Follow Controller and View

public ActionResult Create(PatrimonioModel patrimonio)
    {
        try
        {               
            if (ModelState.IsValid)
            {
                int criaRegistros = CreatePatrimonios(patrimonio.PatControle,
                    patrimonio.PatNumSerie,
                    patrimonio.PatNome,
                    patrimonio.PatDtAquisicao,
                    patrimonio.PatNumNf,
                    patrimonio.PatFornecedor,
                    patrimonio.PatLocFisica,
                    patrimonio.PatValBem,
                    patrimonio.SitCod,                        
                    patrimonio.PatTipoBem
                    );
            }
            return RedirectToAction("Listar");
        }

Change view.

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

Method that fills the Change View.

 public ActionResult Edit(int? id)
    {
        try
        {
            var listaStuacao = LoadSituacaoBem();
            var data = LoadPatrimoniosEditar(id);
            PatrimonioModel patrimonios = new PatrimonioModel();
            foreach (var row in data)
            {
                patrimonios.PatCod = row.PatCod;
                patrimonios.PatControle = row.PatControle;
                patrimonios.PatDtAquisicao = row.PatDtAquisicao;
                patrimonios.PatFornecedor = row.PatFornecedor;
                patrimonios.PatLocFisica = row.PatLocFisica;
                patrimonios.PatNome = row.PatNome;
                patrimonios.PatNumNf = row.PatNumNf;
                patrimonios.PatNumSerie = row.PatNumSerie;
                patrimonios.PatTipoBem = row.PatTipoBem;
                patrimonios.PatValBem = row.PatValBem;
                ViewBag.SitCod = new SelectList(listaStuacao, "SitCod", "SitNome", row.SitCod);
                patrimonios.SitNome = row.SitNome;
            }
            return View(patrimonios);

If I remove Annotation, the date is displayed correctly. How to solve this problem?

  • You didn’t present the view or the edit controller... put the code in the question and not a screen print.

1 answer

0


Above fixed problem already changed the Dataformatstring in Annotation.

    [Display(Name = "Data Aquisição")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime PatDtAquisicao { get; set;}

Browser other questions tagged

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