Querying a Database and Using Display Templates

Learn to implement price-based filtering in ASP.NET Core MVC by creating an action method, querying the Northwind database, and displaying the results in a view.

Let’s create a new action method with a query string parameter passed to it and use that to query the Northwind database for products that cost more than a specified price.

Implementing price-based filtering

We defined a view model containing properties for every value that needed to be rendered. In this example, there will be two values: a list of products and the price the visitor entered. To avoid defining a class or record for the view model, we will pass the list of products as the model and store the maximum price in the ViewData collection. Let’s implement this feature:

Step 1: In HomeController, import the Microsoft.EntityFrameworkCore namespace. We need this to add the Include extension method to include related entities, as we learned earlier.

Step 2: Add a new action method, as shown in the following code:

Get hands-on with 1400+ tech skills courses.