简体   繁体   中英

How to search by date in .NET Core Razor pages?

I first tried with normal sorting and filtering method to search by date but it did not work with datetime datatype and works only with string datatype. It would be great if anyone could hep me figure out how to make this work. Also, I want the same solution to be used for .NET MVC 5. For that, I tried Search date from the detail asp.Net mvc 5 , but for some reason, it is not working for me.

Thank you in advance.

Here is the code:

        //Search by BirthDate
        if (searchStringbyBirthDate != null)
        {
            pageIndex = 1;
        }
        else
        {
            searchStringbyBirthDate = currentFilterBirthDate;
        }
        CurrentFilterBirthDate = searchStringbyBirthDate;

        //Filtering by BirthDate:not working
        if (!string.IsNullOrEmpty(searchStringbyBirthDate))
        {
         personData=personData.Where(b=>b.BirthDate.Equals(searchStringbyBirthDate));
        }

I have it as string here and I know that is wrong but I am unsure how to give datetime here.

Let's imagine you provide 19/04/2000 to your search string and you're looking for a person which was born on this day. Your Person probably have the BirthDate with time (eg 19/04/2000 20:30:49 PM ). However, your search string during conversion to DateTime type will be look like 19/04/2000 0:00:00 AM , because no time was provided. The equality operation works well, these dates are not equal because of time.

So, if you want to search by date only you have to compare only dates. I've created a quick example ASP.NET Core MVC web application below. Take a look on my example controller how I convert both of dates to strings with dates only with ToShortDateString() method.

Example model:

public class ExampleModel
{
     public string Name { get; set; }
     public DateTime Date { get; set; }
}

Example controller:

public class ExampleController : Controller
{
    private IList<ExampleModel> _models = new List<ExampleModel> 
    { 
         new ExampleModel { Name = "Test1", Date = DateTime.Now },
         new ExampleModel { Name = "Test2", Date = DateTime.Now.AddDays(1) },
         new ExampleModel { Name = "Test3", Date = DateTime.Now.AddDays(2) },
         new ExampleModel { Name = "Test4", Date = DateTime.Now.AddDays(3) }
     };

     public ActionResult Index(string search)
     {
        if (!string.IsNullOrEmpty(search))
        {
            if (DateTime.TryParse(search, out var dateTime))
            {
                _models = _models.Where(x => x.Date.ToShortDateString() == dateTime.ToShortDateString()).ToList();
            }
        }

        return View(_models);
    }    
}

Example Index.cshtml:

@model IEnumerable<ExampleModel>

<form asp-action="Index">
    <p>
        Search: <input type="text" name="search">
        <input type="submit" value="Filter" />
    </p>
</form>

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Date)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Date)
                </td>
            </tr>
        }
    </tbody>
</table>

I also recommend to take a look on Getting Started with ASP.NET MVC 5 tutorial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM