It's now time to implement the search feature, allowing customers to put any string inside the search box to look up a product. We need to add one search button to the UI, which when clicked will receive the entered string and fetch records accordingly.
Firstly, the action method needs to accept the search text entered by a customer as a parameter; currently, GetProducts() does not accept any parameter.
The updated GetProducts() should look like the following snippet:
// GET: api/Products
[HttpGet]
public IEnumerable<Products> GetProducts(string searchText)
{
var products = _context.Products.Include(x =>
x.Productsdetail).ToList();
if (!string.IsNullOrEmpty(searchText))
products = products.Where(p => p.Productsdetail
.Any(pd => pd.Name.ToLower().Contains(searchText.ToLower())))
.ToList();
return products;
}
The searchText parameter is...