asp.net-mvc - ASP.NET MVC通过日期时间作为查询

标签 asp.net-mvc

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

我正在建立剧院预订网站,观看表演时,我目前通过表演和地点,但还需要通过日期(因为这是表演的独特组成部分)

ActionResult看起来像这样:

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

但是performanceDate无法使用,因为它是日期时间数据类型!我需要做的是删除数据的时间部分,例如00:00:00并以某种方式将其余数据作为字符串传递,我可以使用它来与之进行比较,如下所示:
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"}) %>

最佳答案

尝试将您的日期以类似以下格式的格式放在查询字符串上:

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

这将为您提供一个URL,例如(如果您不使用路线):
http://foo/Performance/Details?name=someName&venue=someVenue&date=31-Mar-2011

这很有用,因为您要避免在查询字符串上的日期中出现斜线,并且要明确表示月份/日期的位置(整个美国和英国)。建议日期使用数字,月份使用字母字符。

然后,在您的ActionMethod中,您可以简单地TryParse()Parse()输入值。
var dt = DateTime.Parse(date);

在LINQ查询中,您可以执行以下操作:
&& s.performanceDate == dt 

因此,将它们放在一起:
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 - ASP.NET MVC通过日期时间作为查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5615120/

相关文章:

c# - 拥有信用卡模型安全吗?

asp.net - 如何使用 multipart/form-data 进行 ASP.NET MVC Ajax 表单发布?

asp.net-mvc - 使用 GET 的 MVC AttributeRouting - 返回 405 - 方法不允许

c# - 您如何在字符串参数中使用 '/' 处理 ASP.Net Web Api 路由中的路由

javascript - 未捕获的语法错误 : Unexpected token ILLEGAL onclick mvc

asp.net-mvc - ASP.NET MVC 如何使用 ASP.NET Membership Provider 管理用户内容

javascript - 如何在不复制数据的情况下在站点的页面之间共享静态数据?

asp.net-mvc - 允许使用ASP-MVC和Forms Authentication访问特定页面

c# - 在 MVC4 中显示和错误,我必须实现一些接口(interface),但我已经完成了

asp.net-mvc - 为什么ViewBag是空的?