Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Pass datetime as query

Tags:

asp.net-mvc

How would I pass a datetime e.g. 01/01/2011 21:01:34 as a query string?

I am building a theatre booking site and when viewing performances I currently pass the Show and Venue but also need to pass the date (as this is the unique part of the performance)

The ActionResult looks like so:

public ActionResult Details(String name, String venue, String date)
{
    return View(_repository.performanceDetails(name, venue, date));
}

But the performanceDate wont work because it's a datetime data type! What I need to do is REMOVE the time part of the data e.g. 00:00:00 and somehow pass the remaining data as a string that I can use to compare against like so:

public PerformanceDetails performanceDetails(String name, String venue, String date)
{
    var PerformanceDetails = (from s in _db.PerformanceDetails
                              where s.show == name && 
                                    s.venue == venue && 
                                    s.performanceDate == date
                              select s).First();
    return PerformanceDetails;
}

Here is a sample link:

<%= Html.ActionLink("View Details", "Details", "Performances", 
                    new { 
                        name = item.show, 
                        venue = item.venue, 
                        date = item.performanceDate }, 
                    new {@class = "button"}) %>
like image 411
Cameron Avatar asked Apr 10 '11 22:04

Cameron


1 Answers

Firstly, the argument to your Action method should be a DateTime type, not a string. You should make sure that your query string argument is formatted using ISO 8601 Extended Format (http://en.wikipedia.org/wiki/ISO_8601), for example: 2011-10-17T12:35:00.

The default binder will convert that string to a date without any problem. I found that you need two digits each for hh:mm:ss, that is, use leading zeros for numbers below ten. You can omit the milliseconds though, and the time zone 'Z' specifier.

Now that you have a full date and time, you can simply use the date portion for database lookups using mydate.Date.

like image 171
Rob Kent Avatar answered Sep 28 '22 10:09

Rob Kent