Field with Datetime format does not change the format to Date - Asp.net MVC

Asked

Viewed 46 times

1

I need to show only the date format in a Datetime field, but only letters are appearing in the field (even if the value property contains a date).

inserir a descrição da imagem aqui

VIEWMODEL:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
[DataType(DataType.Date, ErrorMessage = "Data em formato inválido")]
[DisplayName("Data de Emissão")]
public DateTime DataEmissao { get; set; }

RAZOR:

<input type="date" asp-for="DataEmissao" id="financeiro-parcela-data-emissao" class="form-control" />

HTML:

<input type="date" id="financeiro-parcela-data-emissao" class="form-control" data-val="true" data-val-required="The Data de Emissão field is required." name="DataEmissao" value="03/11/2020 00:00:00">

Does anyone know how to solve?

Thank you :)

  • It’s from the browser itself

  • Interesting@novic is that if I change type="date" to type="datetime", in Razor, it works correctly, but displaying date + time... I just wanted it to be the date...

  • the datetime even it seems that it is depreciated kkk if I am not mistaken. nonsense this change

1 answer

1

I accidentally discovered the problem.

I was calling a Modal window from another Modal. For some reason, Controller was not creating the Modal prefix that should be "Financeiroparcelaviewmodel". This, for some reason, hindered the display of the dates, because it made as if the fields understood that it was null, when in fact, they were "set" with their respective dates.

Solution:

[HttpGet]
[Route("financeiro-parcela-gerenciar/editar-financeiro-parcela")]
public IActionResult EditFinanceiroParcela(FinanceiroParcelaViewModel financeiroParcelaViewModel)
{
    ViewData.TemplateInfo.HtmlFieldPrefix = "FinanceiroParcelaViewModel"; //Acrescentei esta linha.
    return View(_financeiroParcelaAppService.UpdateAttributes(financeiroParcelaViewModel));
}

Browser other questions tagged

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