Migrate the create action methods to use the repositories for data access and the MovieViewModel for data transport.
- Next up are the Create methods. And just like MVC 5, Core MVC uses a two part process for Create. The GET method returns a view that enables the user to add in a new movie and the POST checks the validity and then executes the save, if everything is valid. The original application returned some sample data in the Create method and we'll leave it there, spelling error and all, but we do need to add in the select list of the genres and we're going to use a ViewBag this time instead of a ViewModel.
So ViewBag.Genres =, and we'll just paste that in from above. It's the same code from before. And then we need to change the model that gets passed into the View into a ViewModel. And that completes the GET action method. Next we'll migrate to POST action method. And there's some comments in here, leftover from the sample.
We'll go ahead and delete those. And the first thing we need to change is the actual signature. It's a little different in Core MVC. It's a lower case i, in the include and it's a parameter, so we change it like that. And that's to prevent overposting. And then we also need to change this from a Movie to a MovieView Model. That's what are were working with in our views. And we'll change the name to movieVm.
Now we want to do a little more, if it's valid, so I'm going to change the format of the if statement. So let's say if ModelState is not valid, then we'll go ahead and return the view to movieVm. And this is, again, just like it was in MVC 5. Now what we can do is just go ahead and delete lines 90 through 97.
And we want to change the MovieViewModel back to a Movie, because that's how we're actually going to save it. So let's define a variable movie=, and we're going to use AutoMapper again to map in the other direction. So we have mapperconfiguration.CreatMapper, and we're going to map to a Movie, from the movieVm.
Now, we need to tell AutoMapper how to do this. Let's go back up to the constructor and we can actually make this into and anonymous method. So we'll do it like this. And we'll add another mapping here, cfg.CreateMap, and then we're just going to flip the types. MovieViewModel and Movie.
Now that we have that done, we can go back into the create POST method. We have a movie, but we've lost the Genre, because in our data design, we have a movie genre class. So we have to GET the Genre from the database and assign it to the MovieGenre property. So we'll say movie.MovieGenre=_movieGenreRepo.Find, and we want to find were GenreName equals the GenreName passed in from the ViewModel and that's just a Genre property.
And then we can add it using the _movieRepo.Add(movie). And then presuming everything works, we just return a RedirectToAction going back to the index. Now the original code had the hard coded string index. I like using the new C# feature. Well, I guess it's not new anymore, the C# 6 feature. nameof, and I can do nameof Index, that way I don't have to worry about magic strings.
Now the advantage of adding the genre this way is we can add in error checking and error handling, but the disadvantage is that we have to remember to do this every time. We can actually tie this into the AutoMapper process using what's called an AfterMap. So we go back up to the constructor. And here on the CreateMap, we're going to add an AfterMap and that takes a source and destination on the left side of the of the lambda and then we say dest.MovieGenre =, and it's the same code we had before.
movieGenreRepo.Find, next to that, GenreName = src.Genre. And now every time it maps from a MovieViewModel to a Movie, it will look up the Genre, based on the string that was passed in. So then we can go down to our create POST method and comment this out. Go ahead and leave it in the code, so that you have both options and you can use which one works best for you.
And that completes the create action methods.
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