ASP.NET MVC通过日期时间作为查询 [英] ASP.NET MVC Pass datetime as query

查看:323
本文介绍了ASP.NET MVC通过日期时间作为查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何传递日期时间,例如01/01/2011 21:01:34作为查询字符串?

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)

ActionResult看起来像这样:

The ActionResult looks like so:

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

但是performanceDate不能工作,因为它是日期时间数据类型!我需要做的是删除数据的时间部分,例如00:00:00并以某种方式将其余数据作为字符串传递,我可以使用它来与之进行比较:

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;
}

以下是示例链接:

<%= Html.ActionLink("View Details", "Details", "Performances", 
                    new { 
                        name = item.show, 
                        venue = item.venue, 
                        date = item.performanceDate }, 
                    new {@class = "button"}) %>

推荐答案

尝试将日期放在查询字符串上,格式如下:

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

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

这将为您提供一个类似URL(如果您不使用路线):

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.

然后在ActionMethod中,您可以简单地TryParse()Parse()输入值.

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

var dt = DateTime.Parse(date);

在LINQ查询中,您可以执行以下操作:

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"}) %>

这篇关于ASP.NET MVC通过日期时间作为查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆