Click here to Skip to main content
15,898,010 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to filter my search by consultant name and date created for records. i am using a datepicker for my calendar and a drop down list for selection on the consultant name. At the moment I have the search working for just the consultant, i cannot figure out how to add the date as a filter.

[HttpPost]
    public ActionResult TimesheetHistroy(Timesheet timesheet)
    {

        ViewBag.ConsultantNum = new SelectList(db.Consultants, "ConsultantNum", "FullName", timesheet.ConsultantNum);
        var timesheets = db.Timesheets.Include(t => t.Client).Include(t => t.Consultant).Where(a=>a.ConsultantNum==timesheet.ConsultantNum && EnterDate == timesheet.CaptureDate) ;
        var r = timesheets.ToList();
        return View(r);
    }


What I have tried:

I have tried using it independently, but i need them to work together
Posted
Updated 27-May-17 18:55pm

1 solution

I'm assuming a.EnterDate is a DateTime or Date type, timesheet.CaptureDate is the string return by the datepicker plugin

You didn't mention what the issue was. I further assume that the code threw error??? because the code is trying to compare a string and a Date?

There are couple of things to look at.

1. The datepicker date format. Make sure you coded it to return date format like 05/28/2017 or however the requirements are.

JavaScript
$(function () {
            $('#CaptureDate').datepicker({
                dateFormat: 'mm/dd/yy' //somehow this return 05/28/2017 vs 05/28/17
            });
        });


2. Here is one of the option. Convert the EnterDate into string with format mm/dd/yyyy and compare it with CaptureDate. Again, I further assumed that the EnterDate does not allow null. If it does, change the code to get the month to "a.EnterDate.Value.Month"

C#
var timesheets = db.Timesheets.Include(t => t.Client).Include(
                t => t.Consultant).Where(a => a.ConsultantNum == timesheet.ConsultantNum &&
         SqlFunctions.Replicate("", 2 - SqlFunctions.StringConvert((double)a.EnterDate.Month).TrimStart().Length) +
         SqlFunctions.Replicate("0", 2 - SqlFunctions.StringConvert((double)a.EnterDate.Month).TrimStart().Length) +
                        SqlFunctions.StringConvert((double)a.EnterDate.Month).TrimStart() +

         SqlFunctions.Replicate("/", 2 - SqlFunctions.StringConvert((double)a.EnterDate.Month).TrimStart().Length) +
         SqlFunctions.Replicate("0", 2 - SqlFunctions.DateName("dd", a.EnterDate).Trim().Length) +
         SqlFunctions.DateName("dd", a.EnterDate).Trim() + "/" +

         SqlFunctions.DateName("year", a.EnterDate) == timesheet.CaptureDate).ToList();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900