Toubi
Toubi

Reputation: 2509

formatting DateTime error "No overload for method 'ToString' takes 1 arguments"

In MVC Razor view, I am trying to format a DateTime field to display time only. Using below code I am getting error "No overload for method 'ToString' takes 1 arguments"

<td>@(Html.DisplayFor(m=>row.LastUpdatedDate).ToString("HH:mm:ss"))</td>

Any help please what is causing this error and how to fix it ?

Thanks for your help.

Upvotes: 3

Views: 4342

Answers (2)

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

DisplayExtensions.DisplayFor returns MvcHtmlString which does not have ToString with one argument thus causing error you see.

You may not even need DisplayFor if you need just to show the date time value:

<td>@row.LastUpdatedDate.ToString("HH:mm:ss")</td>

Upvotes: 2

PSL
PSL

Reputation: 123739

Try use a System.ComponentModel.DataAnnotations.DisplayFormat attribute on the property in the model.

...
[DisplayFormat(DataFormatString = "{0:HH:mm:ss}")]
public DateTime LastUpdatedDate{get; set;}
...

Upvotes: 5

Related Questions