October 2017
Intermediate to advanced
458 pages
11h 13m
English
We are now ready to get to work to replace our repository methods to use AJAX requests to fetch the data from the backend. Let's start with the top three products on the home page. First, we must decide where to put our GetTopProducts function. Since the products are being shown on the home page, HomeController seems like a good spot. The function itself is pretty easy, especially compared to the JavaScript MongoDB callbacks:
public IActionResult GetTopProducts(){ using (var context = new WebShopContext()) { var products = context.Products.Take(3).ToList(); return Json(products); }}
That is really all there is to it. These three lines of code open up a connection to the database, generate a SQL query that is roughly equivalent ...
Read now
Unlock full access