- Now we're going to migrate the create view and this will be a little bit more work than the detailed view. Same as the details, we're going to change the model to a MovieView model and we're going to add in a form tag. And this is, again, using the new tag helpers. action="Create". Now we don't have to put the controller if we're going back to the same controller. I did on the index, just for illustration purposes.
Also going to add in the antiforgery attribute. This is optional, because it defaults to true. So now we can delete the Beginform and the Html Antiforgery token calls. And then we'll move the Closingform tag down to where the closing brace was for the Html Beginform. Scroll back up to the top.
Clean up some of the blank lines. The next thing that we're going to remove is the Html validation summary, because there is a tag helper for that. And what we want to do is set up a div and that's asp-validation-summary= and we'll go with "All", which will show all validation errors. You can also just show the ones that are model specific.
And we'll update the class to "text-danger", which is a bootstrap style, and you see Intellisense helping us there. And then we can delete the Html validation summary. Instead of using the Html.Labelfor, we're going to simply use a label tag, which has a tag helper as well. So let's say label asp-for and that's going to be the "Title" class= and it's the same class that we have in the HTML helper, so we can just copy and paste that.
And close the tag. So we can delete this. Now, what I'm going to do, again, is some clipboard inheritance. And everywhere here there's a label, I will add in the new tag helper version and then go back and set the proper field. And one more to clean up, and that is Rating.
And delete that. Now the HTML editors and labels and all that will still work under Core MBC, you don't have to make this change, but there are some benefits to doing so. So we're going to go ahead and do that for the create as well as the delete and edit, which will be done shortly. Next thing we want to replace are the validation messages. And for an individual field, we use a span.
For the validation summary, we use a div and it's asp-validation-for and that is going to be the field and then class=, again class, text-danger. We see how easy this is compared to trying to remember how to add a class into the HTML helper. And we'll do the same thing, a little copy, paste action.
Change that to ReleaseDate. Paste set in there; change this to Genre. Do the same thing for Price. And finally, the Rating. Okay, next, we're going to replace the Editorfor HTML helpers with input tags. So we just start off simply with input and asp-for and a field name, in this case it's "Title" and then the class="form-control".
And that can be self closing. Now the beauty of the input tag helper is the intelligence built into it. Based on the data type of the property that the tag is for, it will change the HTML elements created. So let's copy this and replace the editor for ReleaseDate. We're going to skip over Genre, because we actually want to do that one a little differently.
This is Price. And Rating. So the final change we want to make is for the Genre. Now the original only had a string value, remember, we've added this so that we can have a select list of genres. So we're going to use the select tag helper and that's asp-for="Genre", and then asp-items=@viewBag.Genres.
This should look familiar, based on what we did for the search. And then finally, it is of type form-control. And then we can delete that. And then, lastly, we go down to the very bottom. We want to change this action link to a tag helper. So a asp-action="Index", there's no route data, the text is Back to list.
And then finally, instead of rendering the jqueryval bundle, we're going to call @Html.RenderPartialAsync and this is going to be our ValidationScriptsPartial. And before we run it, let's just make sure we've cleaned up everything we meant to clean up.
Nope, we missed one. There's and EditorFor we can delete and then this comment leftover from the original sample from Microsoft. We can delete that as well. So save it and run it. So we click on the Movie List. Click on Create New. We'll select Romantic Comedy. Click on Create and it creates the new record. Next we'll move on to the edit view.
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