First MVC Core 2 Project Continuation – Strongly Typed Views


Notice: A non well formed numeric value encountered in /home/bbeveragetrails/public_html/blog/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/bbeveragetrails/public_html/blog/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/bbeveragetrails/public_html/blog/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/bbeveragetrails/public_html/blog/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/bbeveragetrails/public_html/blog/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/bbeveragetrails/public_html/blog/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

post II in the series is here

Creation of a strongly typed view

Now that we have a model for our application we will need to create the form that will use a strongly typed view of the Automobile.cs class. The first thing we want to do is create a second action in our home controller so let’s add the following code to our HomeController.cs

This method calls a view method without an argument, which tells MVC to render the default view associated with the action method, which is a view with the same name as the action method, in this case, AddAutoForm.cshtml so we will need to create this view so let’s create our form to add automobiles in this view. It will be a strongly typed view since it will use the Automobiles model. Create a new view named AddAutoForm.cshtml in the view/Home folder of the project by right clicking on Home
Now name the view AddAutoForm and place this code in the form

The @model Razor expression which is used to create a strongly typed view. A strongly typed view is intended to render a specific model type, and if specify the type I want to work with (@model AutoApp.Models.Automobile in this case) MVC can create some really helpful shortcuts to make it easier. You will also notice the asp-for attributes on the label element sets the value of the for attribute. The asp-for attribute on the input element sets the id and name elements. The asp-action attribute on the form element uses the application’s URL routing configuration to set the action attribute to a URL that will target a specific action method.

We need to add an asp-action that will link to this form in our HWView.cshtml file. The asp-action attribute is an example of the tag helper attribute, which is an instruction for Razor that will be performed when the view is rendered. The asp-action attribute is an instruction to add an href attribute to the element that contains a URL for the action method. This simply tells Razor to insert a URL for an action method defined by the same controller for which the current view is being rendered. It is important to note that you should use the features provided by MVC to generate URLs rather than hard-code them into your views. When the tag helper created the href attribute for the element, it inspected the configuration of the application to be changed to support different URL forms without needing to update any views.

Add the following code to the HWCiew.cshtml file.

This will now link to the new form view!

As this application is now it will not save the form data so if you enter data it just disappears. The next post I will be adding a class to keep the data in memory. I know this is not really useful but it will work for our demonstration purposes.

Leave a Reply

Your email address will not be published. Required fields are marked *