I have an ASP.NET MVC app where I have a column in a table that displays DateTime values from a MySQL database. The column gets sorted by string value instead of DateTime value in my index view. I need it to display like Friday, August 14th, 2020 05:13 PM and sort by date. If i get it to display the way i need it, it doesn't sort and if I get it to sort, it doesn't show the way I need it to.
What I have tried:
I get it to display Friday, August 14th, 2020 05:13 PM by using:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:f}")]
I need it to sort by Date/Time.
This is the code I have.
DataView:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:f}")]
public System.DateTime JobDateTime { get; set; }
Controller:
public ActionResult Index()
{
return View(db.jobs.OrderByDescending(x=>x.JobDateTime).ToList());
}
Index View:
@Html.DisplayFor(modelItem => item.JobDateTime)
I have also tried doing this in my controller:
db.jobs.OrderByDescending(x => x.JobDateTime).Select(d => d.ToString("f")).ToList()
And this:
db.jobs.OrderByDescending(x => x.JobDateTime).ToList().Select(d => d.ToString("f"))
Both of those throw an error saying that there is no overload method for ToString that takes 1 arguments
Any help would be greatly appreciated. Thank you in advance!