2013-10-07 51 views
1

我在搜索日期列表時遇到問題,搜索結果未顯示。變量約會日期已作爲日期時間保存在數據庫中。這是代碼我現在有在我的控制器:難以搜索日期

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]' 
+0

這看起來並不像FORTRAN到我的模型。 –

+0

錯誤在哪裏?在視圖中?拋出控制器?如果你不使用'SearchDate',它會起作用嗎? – JLe

+1

您是否嘗試過在查詢中使用ToList()? – Andrew

回答

1

看起來你想傳遞給你的model不便的List但你正在傳遞查詢,因爲您沒有像這樣枚舉它。

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

    return View(query.ToList()); 
} 

這將有助於如果你給我們看了你的觀點需要

+0

我正在使用約會過程模型的索引視圖。我需要在索引視圖上的列表進行過濾,並只顯示我搜索日期的條目。 – user2764332

+0

hw public class AppointmentProcedure { public int AppointmentProcedureId {get;組; } public int TreatmentId {get;組; } public int InventoryKitId {get;組; } public int EmployeeId {get;組; } public DateTime AppointmentDate {get;組; } public DateTime AppointmentStartTime {get;組; } public DateTime AppointmentEndTime {get;組; } } } – user2764332

+0

請在你的視圖中寫上'@model ....',並且你在'query'上試過'ToList()' –