Marius Vasile

Marius Vasile

  • 594
  • 1.9k
  • 145.7k

asp.net core filter by two dates

Mar 14 2021 3:49 PM
I am trying to filter results of a query using a StarDate and EndDate. My data is saved in format dd/mm/yyyy hh:mm:ss AM/PM and I want to use only date as parameter for filtering. If I use full date format (3/12/2021 12:41:04 PM) is working very well but I want to be able to use only date. What I have is:
 
  1. public IList<WOMain> WOMainL { get; set; }  
  2.         public void OnGet()  
  3.         {  
  4.             WOMainL = _context.WOMains.ToList();  
  5.         }  
  6.   
  7.         public void OnPost(DateTime sdate, DateTime edate)  
  8.         {  
  9.             WOMainL = _context.WOMains.Where(s => s.CreateDate <= sdate && s.CreateDate >= edate).ToList();  
  10.         }  
 
 
  1. <form method="post">  
  2.     Start Date: <input type="datetime" name="sdate" />  
  3.     End Date: <input type="datetime" name="edate" />  
  4.     <input type="submit" value="Submit" class="btn" />  
  5. </form>  
  6.   
  7.   
  8.   
  9. <div class="row no-gutters mt-2">  
  10.     <div class="col-md-1 text-center" style="background-color:firebrick;">  
  11.         <label class="text-white pl-2">WO NUmber</label>  
  12.     </div>  
  13.     <div class="col-md-2 text-center ml-1" style="background-color:firebrick;">  
  14.         <label class="text-white pl-2">Create Date</label>  
  15.     </div>  
  16.     <div class="col-md-2 text-center ml-1" style="background-color:firebrick;">  
  17.         <label class="text-white pl-2">OrgID</label>  
  18.     </div>  
  19.     <div class="col-md-3 text-center ml-1" style="background-color:firebrick;">  
  20.         <label class="text-white pl-2">User ID</label>  
  21.     </div>  
  22.     <div class="col-md-2 text-center ml-1" style="background-color:firebrick;">  
  23.         <label class="text-white pl-2">WOStatus</label>  
  24.     </div>  
  25. </div>  
  26.   
  27.   
  28.   
  29.     @foreach (var item in Model.WOMainL.OrderByDescending(s => s.WONumber))  
  30.     {  
  31.         <div class="row no-gutters mt-2">  
  32.             <div class="col-md-1">  
  33.                 <span class="form-control text-center">@Html.DisplayFor(modelItem => item.WONumber)</span>  
  34.             </div>  
  35.             <div class="col-md-2 ml-1">  
  36.                 <span class="form-control text-center">@Html.DisplayFor(modelItem => item.CreateDate)</span>  
  37.             </div>  
  38.             <div class="col-md-2 ml-1">  
  39.                 <textarea rows="1" class="form-control text-center">@Html.DisplayFor(modelItem => item.OrgID)</textarea>  
  40.             </div>  
  41.             <div class="col-md-3 ml-1">  
  42.                 <textarea rows="1" class="form-control text-center">@Html.DisplayFor(modelItem => item.UserID)</textarea>  
  43.             </div>  
  44.             <div class="col-md-2 ml-1">  
  45.                 <span class="form-control text-center">@Html.DisplayFor(modelItem => item.WOStatus)</span>  
  46.             </div>  
  47.         </div>  
  48.     }  

Answers (2)