日期过滤器无法正常工作MVC

时间:2016-09-18 02:06:18

标签: asp.net-mvc datetime

当我将日期过滤器放入单独的视图时,我的日期过滤器工作得很好,但我无法将其与其他过滤器合并,我不确定我哪里出错了。所有其他过滤器都在工作。您能否请一看,让我知道当我将查询加入其他人以使其工作和合并时我需要更改什么?谢谢!

以下是单独的Controller中的内容,这在视图中起作用:

public ActionResult DateFilter(FormCollection DatePicker)
    {
        DateTime start = DateTime.Today;
        DateTime end = DateTime.Today;

        if (DatePicker.Count > 0)
        {
            start = DateTime.Parse(DatePicker["startDate"].ToString());
            end = DateTime.Parse(DatePicker["endDate"].ToString());
        }
        var Issue = db.Issue.Where(d => d.DateCreated >= start && d.DateCreated <= end).Select(i => new IssueViewModel
        {
            Name = i.Name,
            Description = i.Description,
            DateCreated = i.DateCreated,
            DateCompleted = i.DateCompleted,
            //ect.
        }).ToList();

        ViewBag.Issue = Issue;

        return View();
    }

以下是与其他过滤器合并的内容:

private static IQueryable<Issue> FilterDeviceList(List<Issue> issues, FormCollection DatePicker, string EHP, string IssueKey)
    {
        var query = issue.AsQueryable();

        //COPYING STARTS HERE
        DateTime start = DateTime.Today;
        DateTime end = DateTime.Today;

        if (DatePicker.Count > 0)
        {
            start = DateTime.Parse(DatePicker["startDate"].ToString());
            end = DateTime.Parse(DatePicker["endDate"].ToString());
        }
            query = query.Where(d => d.DateCreated >= start != null && d.DateCreated <= end != null && d.DateCreated == Convert.ToDateTime(DatePicker));

            //COPYING ENDS HERE

        if (!string.IsNullOrWhiteSpace(EHP))
            query = query.Where(i => i.EHPP != null && i.EHPP == (EHP == "1" ? false : true));

        if (!string.IsNullOrWhiteSpace(IssueKey))
            query = query.Where(i => i.IssueKey != null && i.IssueKey.Contains(IssueKey));

return query;
}

如果有人需要查看调用IQueryable的视图或控制器,请告诉我,我可以发布,但我认为这应该足够了。再次感谢你! :)

1 个答案:

答案 0 :(得分:0)

试试这个:

private static IQueryable<Issue> FilterDeviceList(List<Issue> issues, FormCollection DatePicker, string EHP, string IssueKey)
{
    var query = issues.AsQueryable();

    DateTime start,end;

    if (DatePicker.Count > 0)
    {
        start = DateTime.ParseExact(DatePicker["startDate"].ToString(), "MM/dd/yyyy", CultureInfo.InvariantCulture);
        end = DateTime.ParseExact(DatePicker["endDate"].ToString(), "MM/dd/yyyy", CultureInfo.InvariantCulture);
    }
    // assuming start and end date will not be null
    if(start != null && end !=null)
     {
        query = query.Where(d => d.DateCreated >= start && d.DateCreated <= end);
    }  

    if (!string.IsNullOrWhiteSpace(EHP))
        query = query.Where(i => i.EHPP != null && i.EHPP == (EHP == "1" ? false : true));

    if (!string.IsNullOrWhiteSpace(IssueKey))
        query = query.Where(i => i.IssueKey != null && i.IssueKey.Contains(IssueKey));

     return query;
}