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.
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;}
...
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With