0

I have a nullable datetime hat I get from the DB. currently I have my datetime? properties in my model and call them like this in the view to work properly.

<h6>@GTSConnectContent.DateOfLoading: <span>
    @{
        if (@consignmentDetails.DateOfLoading.HasValue)
        { 
        @consignmentDetails.DateOfLoading.Value.ToShortDateString();
        }
    }
</span>
</h6>

But isn't there a way to set it in the actual model so it doesn't have to be set like this in the view. I tried the following but doesn't work

[DisplayFormat(DataFormatString = "yyyy/MM/dd")]
    public DateTime? DateOfLoading { get; set; }
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
JcMey3r
  • 181
  • 2
  • 14
  • possible duplicate of [ASP.Net MVC DisplayFormat](http://stackoverflow.com/questions/2001756/asp-net-mvc-displayformat) – GSerg Apr 08 '14 at 07:29

2 Answers2

2

You have two issues in your code.

1) To use DisplayFormat attribute you have to make use of the DisplayFor method in your view

    @Html.DisplayFor(m => m.DateOfLoading)

This code fragment assumes that you can access DateOfLoading property from your model.

2) You'll need to specify a correct format string as you would in String.Format in the DataFormatString of the DisplayFormat attribute.

    [DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}")]
    [DataType(DataType.Date)]
    public DateTime? DateOfLoading { get; set; }
saintedlama
  • 6,838
  • 1
  • 28
  • 46
  • I needed this for displaying nullable-decimal (`Decimal?`) values in my pages that represented currency values (so I used `[DateType(DataType.Currency)]` and `[DisplayFormat(DataFormatString="{0:C}", ApplyFormatInEditMode=true)]`, however I also needed to add a `DecimalModelBinder` for `typeof(Decimal?)`, from here: http://blog.johnnyreilly.com/2013/03/decimalmodelbinder-for-nullable-decimals.html - finally, I needed to use `Html.EditorFor` instead of `Html.TextBoxFor` in order for the `DisplayFormat` attribute to be respected. – Dai Jun 20 '16 at 07:14
0

How you display the DateTime? value when it is null is related to the View, and not the model. The model only cares what data it holds, it doesn't care how you display the data. Hence, you should not set this display logic in your model. Setting the format of display in the model is fine, but when what to display depends on the value to be displayed, this logic should be decoupled from the model.

Now, if you don't want to write that whole code snippet repeatedly in your View, you could create a helper in your View like this:

@helper DisplayDate(DateTime dt)
{
    if(dt.HasValue)
    {
        dt.Value.ToShortDateString();
    }
    else
    {
        "No Date Available";
    }
}

And then you can use this helper whenever you wish to display the DateTime? value in your View, like this:

<h6>@GTSConnectContent.DateOfLoading: <span>
    @DisplayDate(@consignmentDetails.DateOfLoading)
</span>
</h6>

EDIT:

Here is an introduction to the @helper syntax for your reference

Bhushan Shah
  • 1,028
  • 8
  • 20
  • `The model only cares what data it holds, it doesn't care how you display the data` - false. Which is why there is the `[DisplayFormat]` attribute in the first place. The view may opt to ignore this attribute and dump raw value or format it differently, but by default the MVC helpers obey the annotations from the model, which is the right place to have them. – GSerg Apr 08 '14 at 07:52
  • 1
    @GSerg Wrong choice of words on my part. What I mean is that if the way the data is to be displayed is conditional on the value of the data, this is something that must be taken care of in the display logic in the view. For example, if the value of a DateTime property is greater than a certain value, I wish to display "New" next to where the "Name" property is displayed. Where would you write this logic? I would do it in the View. – Bhushan Shah Apr 08 '14 at 07:57
  • For this kind of logic, yes, it should go to a view. But the OP only wants the date in a certain format, and only uses the cumbersome code because one can't call `ToShortDateString` on a `null` date. Fixing `[DisplayFormat]` would be better here. – GSerg Apr 08 '14 at 08:06