I am trying to create a filter system in C# mvc where i can use multiple input fields for filtering:
View:
- <form asp-controller="Products" asp-action="Index">
- <p>
- Title: <input type="text" name="SearchString" />
- Description: <input type="text" name="SearchString" />
- <input type="submit" value="Filter" />
- </p>
-
- </form>
Model:
- public class Product
- {
-
- public int Id { get; set; }
- public string Title { get; set; }
- public int Price { get; set; }
-
- [Display(Name = "Product description")]
- public string ProductDescription { get; set; }
- public int Stock { get; set; }
- }
Controller:
- public async Task<IActionResult> Index(string searchString, string description)
- {
- var store = from m in _context.Products
- select m;
-
-
-
- if (!String.IsNullOrEmpty(searchString))
- {
- store = store.Where(s => s.Title.Contains(searchString));
- }
-
- if (!String.IsNullOrEmpty(description))
- {
-
- store = store.Where(s => s.ProductDescription.Contains(description));
- }
-
- return View(await store.ToListAsync());
- }
I am not really sure how i can manage this the best possible way