Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Get started with ASP.NET Core and Visual Studio (Part 2)
Lab 5: Get started with ASP.NET Core and Visual Studio (Part 2)
a.
1. 2.
3.
Update the generated pages in an ASP.NET Core app
Estimation time for this section A: 20 Minutes Continue from the Lab 4 exercise.
Now, we will modify and update the header titles and the data type for previous application. Based on the below images, we will change the header of FlowerProducedDate to become the Flower Produced Date (three words) and the header of FlowerName to become the Flower Name (two words). Besides, we also will change the datetime datatype for the column of FlowerProducedDate to become the date datatype.
i. Open the Models/Flower.cs file and add the highlighted lines shown in the following code:
The Display attribute specifies what to display for the name of a field (in this case “Flower Produced Date” instead of “FlowerProducedDate”). The DataType attribute specifies the type of the data (Date), so the time information stored in the field isn’t displayed.
Open the Lab 4 project (MVCFlowerShop) again and continue the steps in this Lab
5.
Level 3
Asia Pacific University of Technology & Innovation Page 1 of 26
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Get started with ASP.NET Core and Visual Studio (Part 2)
ii. Right click on a red squiggly line > Quick Actions and Refactorings.
Visual Studio adds using System.ComponentModel.DataAnnotations.
iii. Right click on a red squiggly line > Quick Actions and Refactorings on the [Column] atribute and select using System.ComponentModel.DataAnnotations.Schema;
iv. Finally, re-run the application again and you will see the below changes in the pages.
Level 3
Asia Pacific University of Technology & Innovation Page 2 of 26
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Get started with ASP.NET Core and Visual Studio (Part 2)
4.
Browse to Pages/Flowers and hover over an Edit link to see the target URL.
The Edit, Details, and Delete links are generated by the Anchor Tag Helper in the Pages/Flowers/Index.cshtml file.
Tag Helpers enable server-side code to participate in creating and rendering HTML elements in Razor files. In the preceding code, the AnchorTagHelper dynamically generates the HTML href attribute value from the Razor Page (the route is relative), the asp-page, and the route id (asp-route- id).
5.
Level 3
Asia Pacific University of Technology & Innovation Page 3 of 26
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Get started with ASP.NET Core and Visual Studio (Part 2)
6.
Now, Use View Source from your favorite browser (google chrome) to examine the generated markup. A portion of the generated HTML is shown below:
The dynamically-generated links pass the Flower ID with a query string (for example, http://localhost:5000/Flowers/Details?id=2).
Now, update the Edit, Details, and Delete Razor Pages to use the “{id:int}” route template. Change the page directive for each of these pages from @page to @page “{id:int}”.
E.g.
7.
Level 3
Asia Pacific University of Technology & Innovation Page 4 of 26
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Get started with ASP.NET Core and Visual Studio (Part 2)
8.
Run the app and then view source. The generated HTML adds the ID to the path portion of the URL:
Posting and binding review. Examine the Pages/Flowers/Edit.cshtml.cs file.
Level 3
Asia Pacific University of Technology & Innovation Page 5 of 26
9.
When an HTTP GET request is made to the Flowers/Edit page (for example, http://localhost:5000/Flowers/Edit/2):
The
OnGetAsync
method fetches the flowers from the database and returns the
Page
method.
The Page method renders the Pages/Flowers/Edit.cshtml Razor Page.
The Pages/Flowers/Edit.cshtml file contains the model directive (@model RazorPagesMovie.Pages.Flowers.EditModel), which makes the flower model available on the page.
The Edit form is displayed with the values from the flower.
When the Flowers/Edit page is posted:
The form values on the page are bound to the Flower property. The [BindProperty] attribute enables Model binding.
If there are errors in the model state (for example, FlowerProducedDate cannot be converted to a date), the form is posted again with the submitted values.
If there are no model errors, the movie is saved.
The HTTP GET methods in the Index, Create, and Delete Razor pages follow a similar pattern. The HTTP POST method in the Create Razor Page follows a similar pattern to the method in the Edit Razor Page.
OnPostAsync
OnPostAsync
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Get started with ASP.NET Core and Visual Studio (Part 2)
b. Add search to ASP.NET Core Razor Pages.
Estimation time for this section B: 30 Minutes
In this document, search capability is added to the Index page that enables searching flowers by type or name.
1.
Add the following highlighted properties to Pages/Flowers/Index.cshtml.cs:
You’ll work with the Typess and FlowerTypes properties later in this document. Update the Index page’s OnGetAsync method with the following code:
2. 3.
SearchString: contains the text users enter in the search text box.
Types: contains the list of types. This allows the user to select a genre from the list.
FlowerTypes: contains the specific types the user selects (for example, “Asteraceae”).
Level 3
Asia Pacific University of Technology & Innovation Page 6 of 26
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Get started with ASP.NET Core and Visual Studio (Part 2)
4.
Navigate to the Flowers page and append a query string such as ?searchString=Chry to the URL (for example, http://localhost:5000/Flowers?searchString=Chry). The filtered flowers are displayed.
If the following route template is added to the Index page:
The search string can be passed as a URL segment (for example, http://localhost:5000/Flowers/Chry).
However, you can’t expect users to modify the URL to search for a flower. In this step, UI is added to filter flowers. If you added the route constraint “{searchString?}”, remove it.
Open the Pages/Flowers/Index.cshtml file, and add the