osyris zosar

osyris zosar

  • NA
  • 289
  • 26.7k

Return View with multiple parameter

Feb 28 2021 6:24 AM
I am trying to create a filter system in C# mvc where i can use multiple input fields for filtering:

View:
 
  1. <form asp-controller="Products" asp-action="Index">  
  2.     <p>  
  3.         Title: <input type="text" name="SearchString" />  
  4.         Description: <input type="text" name="SearchString" />  
  5.         <input type="submit" value="Filter" />  
  6.     </p>  
  7.   
  8. </form>  
Model:
 
  1. public class Product  
  2. {  
  3.       
  4.     public int Id { get; set; }  
  5.     public string Title { get; set; }  
  6.     public int Price { get; set; }  
  7.   
  8.     [Display(Name = "Product description")]  
  9.     public string ProductDescription { get; set; }  
  10.     public int Stock { get; set; }  
  11. }
 
 
 
Controller:
 
  1. public async Task<IActionResult> Index(string searchString, string description)  
  2. {  
  3.     var store = from m in _context.Products  
  4.                    select m;  
  5.   
  6.      
  7.   
  8.      if (!String.IsNullOrEmpty(searchString))  
  9.     {  
  10.        store = store.Where(s => s.Title.Contains(searchString));  
  11.     }  
  12.   
  13.     if (!String.IsNullOrEmpty(description))  
  14.     {  
  15.   
  16.        store = store.Where(s => s.ProductDescription.Contains(description));  
  17.     }  
  18.   
  19.     return View(await store.ToListAsync());  
  20. }  
 I am not really sure how i can manage this the best possible way
 

Answers (6)