[英]Filter Data by date range c# mvc4
第一張卡片: CardId, Description
。 -保留所有可用卡的記錄第二個表RegistartionDt: RegistartionDtId, RegisteredDateTime , CardId
。 保留時間數據和使用特定卡的數據。
我想做一個搜索功能,可以按日期搜索卡。 例如,用戶將從下拉菜單中選擇日期。 開始日期和結束日期,並搜索該范圍內使用的卡片。
到目前為止,我有:
public ActionResult List ()
{
CardDBEntities _db = new CardDBEntities();
var cards = (from c in _db.Cards
join d in _db.RegistrationDTs
on c.CardId equals d.CardId
where d.RegistrationDateTime >= startDate &&
d.RegistrationDateTime <= endDate
select new
{
d.CardId,
c.Description
}).OrderByAscending(x => x.RegistrationDateTime)
.ToList();;
return View(cards);
}
我該怎么辦? 如何顯示我的結果? 應該如何定義endDate和startDate我沒有在任何地方定義它們。 我希望結束日期和開始日期是用戶將在搜索框中插入的值。
請幫助。 謝謝
您可以嘗試這樣的事情:
// Declare a model class in your models folder, like below
// You could give any name you want to this class
public class ModelName
{
public int CardId { get; set; }
public string Description { get; set; }
public DateTime RegistrationDateTime { get; set; }
}
然后在控制器內部,可以將方法重構為以下方法:
public ActionResult List (DateTime startDate, DateTime endDate)
{
using(var db = new CardDBEntities())
{
var cards = (from c in db.Cards
join d in db.RegistrationDTs
on c.CardId equals d.CardId
where d.RegistrationDateTime >= startDate &&
d.RegistrationDateTime <= endDate
select new ModelName
{
CardId = d.CardId,
Description = c.Description,
RegistrationDateTime = d.RegistrationDateTime
}).OrderByAscending(x => x.RegistrationDateTime)
.ToList();
ViewData["cards"] = cards;
return View();
}
}
應該如何定義endDate和startDate我沒有在任何地方定義它們。
您可以在ActionResult
方法中將它們定義為參數。
我該怎么辦?
您可以從ViewBag
集合中讀取數據。
我該怎么辦?
那完全取決於您想表達結果的方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.