6

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"}) %>
Branislav Abadjimarinov
  • 5,101
  • 3
  • 35
  • 44
Cameron
  • 27,963
  • 100
  • 281
  • 483

3 Answers3

8

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.

Rob Kent
  • 5,183
  • 4
  • 33
  • 54
4

Try putting your date on the querystring in a format something like this:

item.performanceDate.ToString("dd-MMM-yyyy")

This will give you a URL like (if you're not using routes):

http://foo/Performance/Details?name=someName&venue=someVenue&date=31-Mar-2011

This is helpful because you'll want to avoid slashes in your date on the querystring, and you'll want to be explicit about month/date placement (the whole US and UK). Suggest using numbers for date, and alpha characters for month.

In your ActionMethod, you can then simply TryParse() or Parse() the incoming value.

var dt = DateTime.Parse(date);

In your LINQ query you can do something like this:

&& s.performanceDate == dt 

So to put it all together:

public ActionResult Details(String name, String venue, String date)
{
    DateTime dt;

    if (DateTime.TryParse(date, out dt))
    {
        return View(_repository.performanceDetails(name, venue, dt));    
    }
    else
    {
        return View("Error", "The value passed in date isn't a date value.");
    }
}

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

}

<%= Html.ActionLink("View Details", "Details", "Performances", 
                new { name = item.show , 
                      venue = item.venue, 
                      date = item.performanceDate.ToString("dd-MMM-yyyy") }, 
                new {@class = "button"}) %>
p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • I'm **NOT** routing urls though so I currently have urls like `/?name=Macbeth&venue=Playhouse&date=02/01/2011` – Cameron Apr 10 '11 at 22:28
  • @p.campbell Sorry I'm a bit confused (quite late here in the UK too) any chance you could edit my three pieces of code with your idea so I can fully see what you mean. I think I do but I'm worried about blowing up my app. **MUCH APPRECIATED!** – Cameron Apr 10 '11 at 22:35
  • I get the following error: `The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.` and it blows up in my repository (so the second piece of code) any ideas what the problem is and how to fix it? Thanks – Cameron Apr 10 '11 at 22:51
  • it's an MDF in the App_Data folder it's a datetime and it's NOT nullable. thanks – Cameron Apr 10 '11 at 22:56
  • Get this error now: `LINQ to Entities does not recognize the method 'System.DateTime AddDays(Double)' method, and this method cannot be translated into a store expression.` – Cameron Apr 10 '11 at 23:03
  • YES! Thanks so much :P Was getting really depressed then. Thanks for all the help. – Cameron Apr 10 '11 at 23:09
2

There are several ways you can do this. One way is the way Cameron suggests. Another way would be to convert the date to a numeric number, such as ticks, then pass this numeric value on your URL and convert it back in your Action method.

Int64 ticks = dateOfShow.Ticks;

DateTime newDateOfShow = new DateTime(ticks);
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291