bnil
bnil

Reputation: 1541

DateTime formatting error in the MVC view

I am developing MVC app. I am trying to format the date in view. In one statement it working fine , but in another statement showing the error.

Below statement work fine.

 @Model.CreatedDate.ToString("dd-MMM hh:mm tt")

This statement generates an error.

@Model.ModifiedDate.ToString("dd-MMM hh:mm tt")

Error is

no overload method takes 1 string

Got the soultion

Thanks to mattytommo

In Model I have set the property "Nullable" so I have to change the statement to

@Model.ModifiedDate.Value.ToString("dd-MMM hh:mm tt")

Its working now...

Upvotes: 1

Views: 455

Answers (1)

Mathew Thompson
Mathew Thompson

Reputation: 56449

I'm guessing ModifiedDate is nullable. If that's the case, try this (although you may want to perform null checking, otherwise it'll throw an error for null values):

@Model.ModifiedDate.Value.ToString("dd-MMM hh:mm tt")

Upvotes: 4

Related Questions