Core MVC has added several helper methods for returning HTTP results. In this video, learn how to update the details action method to use the repositories and AutoMapper to populate the ViewModel. See how to change the error handling to take advantage of the helper methods to return NotFound or BadRequest code.
- [Voiceover] Continuing down our controller, there is an example here from the training module. Let's go ahead and delete that because it doesn't apply to what we're doing. The next action method is the details action method. And we'll save it. I am going to just reformat, there we go. And this is pretty close to the same. There are some helper methods in the controllers for Core MVC. And I can just return a new bad request.
We'll do the same thing down here, and change this, instead of a http not found, this is a simply not found, some minor changes between MVC 5 and Core MVC. And we also want to change to user repository instead of the context directly, so we do that by movieRepo.find, and we're going to find by ID, but we also want to include the genre.
Remember, the original data model just had one object. Now we have related objects. There isn't any lazy loading at this point in entity framework core, so if we want to get that genre and name, we have to pass in include in the EF link statement. So we say, find where x.ID equals ID, and include x.MovieGenre.
And just as a quick reminder, let's go into the find method. See here, I'm taking in a where expression, right here on line 45, then an include expression, and then I simply call table.where, with the where clause .Include, first or default. This is just my flavor of repo methods, that I find typically pretty helpful, not required by any means, but find and include is however you decide to implement it.
Now, we also want to return a view model, instead of just a movie, so then, what we can do is, set up a new var, and that is going to be mapper.Configuration, create mapper, and we're going to map, instead of a list this time, we're going just straight to the movie view model, passing in the movie.
And then we return, instead of the movie, the view model. And that completes our details action method.
Phil Japikse begins by showing how to install and update the .NET Core SDK. He reviews the functionality of the MVC 5 app, explains how to create necessary projects, and discusses migrating static content. Next, he demonstrates how to create a data access layer, complete the server-side migration, set up the HTTP pipeline, add custom items into the dependency injection container, leverage the new project configuration system, and migrate the controllers. Phil then introduces Tag Helpers—one the big new features in ASP.NET Core—and uses them to migrate and simplify the views. Phil also demonstrates how to create and use custom Tag Helpers. To wrap up, he covers working with view components, explaining what they are and why they're helpful. He walks through how to create the server-side view component code, and how to refactor your app and invoke the view component.
- Reviewing the MVC 5 application
- Creating the data access layer
- Adding and updating the models
- Updating the database
- Completing the server-side migration
- Configuring the HTTP pipeline
- Configuring and using dependency injection
- Migrating the views
- Creating view components