Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date Format using Html.DisplayFor() in MVC5

Referencing the answer in this post I added /Views/Shared/DisplayTemplates and added a partial view called ShortDateTime.cshtml as shown below:

@model System.DateTime
@Model.ToShortDateString()

When the model contains a value this works and the formatted date is displayed correctly:

@Html.DisplayFor(modelItem => item.BirthDate, "ShortDateTime")

However, if a null value is returned a 'System.InvalidOperationException' is thrown. Indicating:

{"The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'."}

My first inclination was to use an if statement inside the partial view but it didn't seem to matter. Without referencing the template null values are handled as in:

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

but the original issue of formatting remains. When I try to put conditional formatting in the View as follows, it doesn't work but I hoping it's just a syntax thing.

@Html.DisplayFor(modelItem => item.BirthDate == null) ? string.Empty : (modelItem => item.BirthDate, "ShortDateTime"))

The above results in a different 'System.InvalidOperationException':

{"Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions."}

So, is there a way to do conditional formatting in the View to generate just the date from a DateTime value?

like image 483
Greg Hayden Avatar asked Nov 12 '13 03:11

Greg Hayden


People also ask

What is HTML DisplayFor?

DisplayFor() The DisplayFor() helper method is a strongly typed extension method. It generates a html string for the model object property specified using a lambda expression.

How do I format a date in HTML table?

php $orgDate = "2019-09-15"; $newDate = date("d-m-Y", strtotime($orgDate)); echo "New date format is: ". $newDate. " (MM-DD-YYYY)"; ?>

How do I change the date format in razor view?

You can use the DisplayFormat data annotation attribute on the model property to specify the format and ensure that the format also applies when the value is in "edit mode" (a form control): [BindProperty, DisplayFormat(DataFormatString = "{0:yyyy-MM-ddTHH:mm}", ApplyFormatInEditMode = true)]

How can get date in dd mm yyyy format in asp net?

The HTML Markup consists of an ASP.Net GridView with three BoundField columns. The third BoundField column is bound to a DateTime field and the DataFormatString property is set to {0:dd/MM/yyyy} in order to display the DateTime field value in dd/MM/yyyy format.


2 Answers

The problem you're experiencing is that you are passing a null value into a non-nullable model. Change the partial view's model to DateTime?. For example:

@model DateTime?          
@if (!Model.HasValue)
    {
    <text></text>
}
else
{
    @Model.Value.ToShortDateString()
}

Hope this helps.

like image 62
Vinney Kelly Avatar answered Oct 04 '22 11:10

Vinney Kelly


Have you tried this?

@if (modelItem.BirthDate != null) { Html.DisplayFor(modelItem => item.BirthDate, "ShortDateTime") }
like image 45
Wagner DosAnjos Avatar answered Oct 04 '22 12:10

Wagner DosAnjos