谁能告诉我哪里出了问题。我有一个错误列表,我希望能够按日期搜索。例如,如果我从日期选择器中选择了日期 20/08/2015,那么我希望列表只显示该日期的错误。 By default it will show all the errors in the database, but when a date is selected, I want to show only the errors from that date.
Controller
[HttpPost]
public ActionResult Index(string sortOrder, int? page, DateTime? datePicker)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
DateTime userSelectedDate = DateTime.Parse(Request["datePicker"]);
var applications = from s in db.ElmahErrors
where s.TimeUtc == userSelectedDate
select s;
switch (sortOrder)
{
default:
applications = applications.OrderByDescending(x => x.TimeUtc);
break;
}
int pageSize = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["DefaultPageSize"]);
int pageNumber = (page ?? 1);
return View(applications.ToPagedList(pageNumber, pageSize));
}
查看
@using (Html.BeginForm())
{
<label for="datePicker">Find By Date:</label>
@Html.TextBox("datePicker", null, new { @type = "date" })
<input id="submitBtn" type="submit" value="Search" />
}
userSelectedDate
变量正在从日期选择器中获取值,但它不匹配任何错误。有人可以帮忙吗?
提前致谢。
最佳答案
您是根据 DateTime 而不是 Date 进行选择,因此记录必须准确地发生在 DateTime 变量设置的时间(精确到毫秒)。要执行您想要的操作,您需要对选择查询做更多的操作以仅匹配日期部分。有几种方法可以做到这一点,但是这个 SO 问题很好地总结了它们:
How to compare only date components from DateTime in EF?
本质上,最简单的方法是在谓词中的两个日期上使用 EntityFunctions.TruncateTime 方法。
关于c# - 按日期显示结果 ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32220862/