【问题标题】:ASP.NET MVC Pass datetime as queryASP.NET MVC 将日期时间作为查询传递
【发布时间】:2013-02-20 18:36:19
【问题描述】:

我将如何传递日期时间,例如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"}) %>

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    首先,Action 方法的参数应该是 DateTime 类型,而不是字符串。您应确保查询字符串参数使用 ISO 8601 扩展格式 (http://en.wikipedia.org/wiki/ISO_8601) 进行格式化,例如:2011-10-17T12:35:00。

    默认的活页夹会毫无问题地将该字符串转换为日期。我发现 hh:mm:ss 每个都需要两个数字,也就是说,对十以下的数字使用前导零。不过,您可以省略毫秒和时区“Z”说明符。

    现在您有了完整的日期和时间,您可以使用 mydate.Date 简单地将日期部分用于数据库查找。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • NOT 路由 URL,所以我目前有像 /?name=Macbeth&amp;venue=Playhouse&amp;date=02/01/2011 这样的 URL
      • @p.campbell 对不起,我有点困惑(在英国也很晚了)任何机会你可以用你的想法编辑我的三段代码,这样我才能完全明白你的意思。我想我会,但我担心炸毁我的应用程序。 非常感谢!
      • 我收到以下错误:The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported. 它在我的存储库中爆炸(所以第二段代码)任何想法是什么问题以及如何解决它?谢谢
      • 它是 App_Data 文件夹中的 MDF,它是一个日期时间,它不能为空。谢谢
      • 立即获取此错误:LINQ to Entities does not recognize the method 'System.DateTime AddDays(Double)' method, and this method cannot be translated into a store expression.
      【解决方案3】:

      有几种方法可以做到这一点。一种方法是卡梅伦建议的方式。另一种方法是将日期转换为数字,例如刻度,然后在您的 URL 上传递此数值,并将其转换回您的 Action 方法。

      Int64 ticks = dateOfShow.Ticks;
      
      DateTime newDateOfShow = new DateTime(ticks);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-16
        • 1970-01-01
        • 1970-01-01
        • 2019-11-19
        • 1970-01-01
        • 2017-10-17
        • 1970-01-01
        • 2023-03-12
        相关资源
        最近更新 更多