0

I wanted to create a dropdown, to select the price Range of the books and I have made a table to store the range values and from this table I am selecting the Ids of the range in the dropdown. Here my purpose is to get the list of books in that price range. Basically I am applying filter by price range on a list of books.
Here is my code:

This is controller part:

 public async Task<IActionResult> Index(string searchString, int? priceId)
    {
        IQueryable<int> priceQuery = from p in _context.BookPriceModel
                                     orderby p.PriceId
                                     select p.PriceId;

        var books = from m in _context.BookModel
                    select m;
        var prices = from p in _context.BookPriceModel
                     select p;

        if (priceId != null)
        {
            BookPriceModel price = new BookPriceModel();
            foreach (var x in prices)
            {
                if (x.PriceId == priceId)
                {
                    price = x;
                }
            }

            books = books.Where(s => s.Price < price.MaxPrice);
        }

        if (!String.IsNullOrEmpty(searchString))
        {
            books = books.Where(s => s.BookName.Contains(searchString));
        }
        

        var bookVM = new BookPriceViewModel
        {
            PriceRange = new SelectList(await priceQuery.Distinct().ToListAsync()),
          
            Books = await books.ToListAsync()
        };

        var VM = new ViewModel
        {
            VM1 = bookVM,
            VM2 = await prices.ToListAsync()
        };

        return View(VM);
    }

/This is the view

<form asp-controller="BookModels" asp-action="Index">
     <p>
         <select asp-for="VM1.PriceId" asp-items="Model.VM1.PriceRange">
             <option value=""></option>
         </select>

    Book Name: <input type="text" name="SearchString" />
    <input type="submit" value="Filter" />
</p>

Please help me to find the mistake that I am making here.

0 Answers0