VALIDATE AND PERSIST DATA

DESIGNING A TASK-BASED UI

NET COREWe are going to look at how data insertion, editing and deletion are accomplished. Let’s start with the course entry form, and immediately we have to ask ourselves what data to ask the user for? If we present all the information to be entered at once, the user might be disoriented with all the amount of data to be entered, then we could break this amount of data into smaller Forms, so that the user enters the data that our application needs gradually.

Titolo

After the title is entered, which is perhaps the most important information, we will provide an Edit Data Form.

Modifica

Once the course is created we will create a Form for entering lessons.

Lessons

And lesson modification.

Lessons modifica

CREATE THE DATA ENTRY FORM

The author’s full name should not be given at all at this stage because there could be a malicious user who does an impersonation. The first and last name will be entered at the authentication and authorization steps. By the time the lecturer arrives on this screen he or she will have logged in; therefore, we will know who he or she is, and whether he or she is authorized to be a lecturer in which case he or she must have provided a resume.

Nuovo Corso

Let’s look at the View first then the Controller.

Create.cshtml
Create Controller

Let us understand how the asp-for tag works. Let’s say right away that it doesn’t do anything magical, it adds the name and id attributes; however, if instead of typing Title we type TITLE we would get an error because in CourseCreateInputModel there is no property with that name. The image below is the newly created form.

asp-for
Form di Inserimento
Controller

We wrote in the CoursesController two Actions having the same name, but one handles GET requests from the Form; therefore, it is the one that provides Model input to the View making it strongly typed. The moment we type something in the text box and Submit the Form a new request is triggered this time in POST correctly involving the Routing mechanism so that in the inputModel parameter of type CourseCreateInputModel we read what the user typed.

Disambiguazione delle action
Button Create

LINK TO CODE ON GITHUB

GITHUB

Download the section code15 or clone the GITHUB repository to have all sections available in your favorite editor.