Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!
Posted
Updated 16-Aug-20 5:52am
Comments
[no name] 16-Aug-20 7:07am    
Naive question: Why not already sort when data are retrieved from mySQL?

1 solution

db.jobs.OrderByDescending(x => x.JobDateTime).Select(d => d.JobDateTime.ToLongDateString()).ToList()
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900