Field NullableDateTime 01/01/0001 [closed]

-1

I have a Model field

[Column("sdt_dataPagamento")]
[Display(Name = "Dt. Pagamento")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? DataPagamento { get; set; }

I'm displaying this field in the View but the null values are 01/01/0001

As I have tried to solve this problem.

@if (item.DataPagamento.HasValue)
{
    item.DataPagamento.Value.ToShortDateString();
    <span>teste</span>
}

The teste appears but the date does not.

If I do this:

  @Html.DisplayFor(modelItem => item.DataPagamento)

Appears in 2016-04-12 (aaaa-mm-dd) format

I've also tried

@if(item.DataPagamento.Value.Year != 0001
@if(item.DataPagamento != null)
@if(item.DataPagamento != DateTime.MinValue)
//entre outras tentativas

I want to display only Date dd / mm / yyyy

I would not like to modify the attributes of the model because it could present errors in other views, I know it is not the correct model view in the view, it should create a ViewModel, but in practice I do sometimes when I think it is simple. I would only modify Model if it is really wrong and is the only way.

    
asked by anonymous 20.04.2016 / 19:19

1 answer

3

A @ was missing here:

@if (item.DataPagamento.HasValue)
{
    @item.DataPagamento.Value.ToShortDateString();
    <span>teste</span>
}
    
20.04.2016 / 19:46