-1

i want to filter or search between 2 date i write this code , but when i run the application the data which in the table not show what is the wrong ?

This is my controller :

    public ActionResult Index(DateTime? start, DateTime? end)
    {
        var ExpenseDetails = _context.ExpenseDetails.Include(s => s.expenses).Where(t => t.DateExpense >= start && t.DateExpense <= end).ToList();

        return View(ExpenseDetails);
    }

and this is View :

@using (Html.BeginForm())
{
        <div>
            <span>Start Date :</span> <input type="date" name="start" />
            <span>End Date :</span> <input type="date" name="end" />
            <input type="submit" value="Get Records Between Dates" />
        </div>
        <table>
        @foreach (var item in Model)
        {
            <tbody>
               <tr>
                    <td>
                         @Html.DisplayFor(modelItem => item.expenses.Expenses_Type)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.DateExpense)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Amount)
                    </td>                           
            </tbody>
         }
        </table>

}

enter image description here

enter image description here

Thank you

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
programmer
  • 49
  • 10

1 Answers1

1

You need to confirm the followings when you are dealing with dates:

  • Compare without the time part if not required. Example is here
  • Compare DateTime in a common format. For example input DateTime and server DateTime can be on different timezone. Convert them both to Utc before the comparison
  • You can compare the year and months individually [not recommended]
Rahatur
  • 3,147
  • 3
  • 33
  • 49
  • Can you tell me how the input in the date boxes on the UI are populated? Do you type it in or use a plugin or browser's default behavior to populate the fields? Also on post-back what are the values in the 'start' and 'end'. And what is the value in the t.DateExpense? Can you share some screenshots in debug mode? – Rahatur Apr 19 '19 at 12:25
  • The user enter the date for example ; user enter the date 1 in text box 1 and then enter the date 2 in text box 2 then all the data which is between this date will be show – programmer Apr 19 '19 at 13:36
  • That much I understood. I was looking for the date format. dd/mm/yyyy or mm/dd/yyyy or d/m/yy? Please provide some screenshots in debug mode. – Rahatur Apr 19 '19 at 13:48
  • OK i will add pic – programmer Apr 19 '19 at 14:10
  • Can check now , this what try to do – programmer Apr 19 '19 at 14:22
  • @programmer can you share the screenshot of the debug mode? Need to see the value in the variables. From the UI screenshot it is not clear what are the values in the server side variables. – Rahatur Apr 20 '19 at 05:17