【问题标题】:having difficulty searching for a date难以寻找约会对象
【发布时间】:2013-10-07 11:27:19
【问题描述】:

我在搜索日期列表时遇到问题,没有显示搜索结果。可变约会日期已作为日期时间保存在数据库中。这是我目前在我的控制器中的代码:

public ActionResult Index(DateTime SearchDate)
{
    var query = from a in db.AppointmentProcedures
                where a.BookingStatus == false
                orderby a.AppointmentStartTime
                select a;

    if (SearchDate != null)
    {
        query = from a in db.AppointmentProcedures
                orderby a.AppointmentDate
                where a.AppointmentDate <= SearchDate
                select a;
    }

    return View(query);
}

这是我继续遇到的错误:

The model item passed into the dictionary is of type
'System.Data.Entity.Infrastructure.DbQuery`1[System.DateTime]', but this dictionary
requires a model item of type
'System.Collections.Generic.IEnumerable`1[LaserculptFinal.Models.AppointmentProcedure]'

【问题讨论】:

  • 在我看来这不像 FORTRAN。
  • 错误在哪里?在视图中?扔进控制器?如果不使用SearchDate,它是否有效?
  • 您是否尝试过在查询中使用 ToList()?
  • 是的,我已经尝试过 ToList 并且给我构建错误的代码行是: where a.AppointmentDate == Convert.ToString(SearchDate)

标签: c# asp.net-mvc search


【解决方案1】:

看起来你想传递给你的 model 一个 List smth 但是你传递了一个查询,因为你没有像这样枚举它。

public ActionResult Index(DateTime SearchDate)
{
    .....    

    return View(query.ToList());
}

如果您向我们展示了您的视图需要的模型,将会有所帮助

【讨论】:

  • 我正在使用约会程序模型的索引视图。我需要索引视图上的列表进行过滤,并且只显示我正在搜索的日期的条目。
  • hw 公共类 AppointmentProcedure { public int AppointmentProcedureId { get;放; } 公共 int TreatmentId { 得到;放; } 公共 int InventoryKitId { 获取;放; } 公共 int EmployeeId { 获取;放; } 公共日期时间约会日期 { 获取;放; } 公共日期时间 AppointmentStartTime { 获取;放; } 公共日期时间 AppointmentEndTime { 获取;放; } } }
  • 请在@model.... 行中显示您的视图中写入的内容,您是否在query 上尝试过ToList()
  • 不是实体模型(我假设您使用它)而是您传递给 MVC 视图的模型类
  • @model IEnumerable
猜你喜欢
  • 2012-09-02
  • 1970-01-01
  • 2014-10-21
  • 2013-12-10
  • 2016-12-26
  • 2023-01-30
  • 2019-12-13
  • 2016-12-02
  • 2020-10-01
相关资源
最近更新 更多