A
A
authoraytee2020-06-25 15:02:25
MySQL
authoraytee, 2020-06-25 15:02:25

How to do search by date in ASP.Net MVC 5?

I can’t set up a search for a record by date in the application

. I have 3 fields:
5ef490f14aeb3004786777.png

Search by customers and material works as it should, I wrote as:

[HttpPost]
        public ActionResult OrdersByCustomers(string name)
        {
            var allOrdersByCustomers = db.Orders
                .Where(a => a.Customer.CustomerName.Contains(name))
                .Include(p => p.Customer)
                .Include(p => p.Material)
                .ToList();

            if (allOrdersByCustomers.Count >= 0)
            {
                return View(allOrdersByCustomers);
            }
            return View();
        }

        [HttpPost]
        public ActionResult OrdersByMaterials(string name)
        {
            var allOrdersByMaterials = db.Orders
                .Where(a => a.Material.Name.Contains(name))
                .Include(p => p.Customer)
                .Include(p => p.Material)
                .ToList();

            if (allOrdersByMaterials.Count >= 0)
            {
                return View(allOrdersByMaterials);
            }
            return View();
        }


But I can't do a search by date, I'm trying to write like:
[HttpPost]
        public ActionResult OrdersByDate(DateTime date)
        {
            var allOrdersByDate = db.Orders
                .Where(a => a.Date.Contains(date)) //В этом моменте дает ошибку, говорит, что дата не может Contains
                .Include(p => p.Customer)
                .Include(p => p.Material)
                .ToList();

            if (allOrdersByDate.Count >= 0)
            {
                return View(allOrdersByDate);
            }
            return View();
        }


How can I implement a search by date if you do it the way I do, or if you have any suggestions on how to search by all features through one field - please answer ... I

have little experience, but there are no other ideas, so I ask for help here

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2020-06-25
@kttotto

Date search must be range or exact match

allOrdersByDate = db.Orders.Where(a => a.Date >= dateFrom && a.Date <= dateTo)

allOrdersByDate = db.Orders.Where(a => a.Date == date)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question