Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.
Welcome back! If you missed part one where we build out our backend, check it out here!
Create the Angular Service
We will create an Angular service which will convert the Web API response to JSON and pass it to our component. Right click on ClientApp/app folder and then Add >> New Folder and name the folder Services.
Right click on Services folder and select Add >> New Item. An "Add New Item" dialog box will open. Select Scripts from the left panel, then select "TypeScript File" from the templates panel, and set the name as empservice.service.ts. Press OK
Open the empservice.service.ts file and put the following code into it.
At this point in time, you might get the error. "Parameter 'employee' implicitly has an 'any' type", in the empservice.service.ts file.
If you encounter this issue, then add the following line inside tsconfig.json file.
"noImplicitAny": false
Now, we will proceed to create our components.
Creating Angular Components
We will be adding two Angular components to our application:
- The
fetchemployee
component - will display all employee data and delete an existing employee's data. - The
addemployee
component - will add a new employee's data or edit an existing employee's data.
Right click on ClientApp/app/components folder and select Add >> New Folder and name the folder, addemployee.
Right click on addemployee folder and select Add >> New Item. An "Add New Item" dialog box will open. Select Scripts from the left panel, then select " TypeScript File" from templates panel, and put the name as addemployee.component.ts. Press OK. This will add a typescript file inside addemployee folder.
Right click on addemployee folder and select Add >> New Item. An "Add New Item" dialog box will open. Select ASP.NET Core from the left panel, then select "HTML Page" from templates panel, and put the name as addemployee.component.html. Press OK. This will add an HTML file inside the addemployee folder.
Similarly, create a fetchemployee folder inside the ClientApp/app/components folder and add fetchemployee.component.ts and fetchemployee.component.html file to it.
Now our ClientApp/app/components will look like the image below:
Open the fetchemployee.component.ts file and put the following code in it:
Let's understand this code. At the very top, we have imported Angular modules and EmployeeService references. After this, we have the
@Component
decorator to define the template URL for our component.
Inside the
FetchEmployeeComponent
class, we have declared an array variable empList of type EmployeeData where EmployeeData is an interface having the properties same as our TblEmployeeModel class. Inside the getEmployees method, we are calling the getEmployees
method of our service EmployeeService
, which will return an array of employees to be stored in the empList
variable. The getEmployees method is called inside the constructor so that the employee data will be displayed as the page loads.
Next, we have a
delete
method which accepts employeeID
as a parameter. This will prompt the user with a confirmation box and if the user selects yes then it will delete the employee with this employeeID.
Open fetchemployee.component.html file and put the following code into it.
The code for this HTML file is pretty simple. At the top, it has a link to create new employee record and after that, it will have a table to display employee data and two links for editing and deleting each employee record.
We are finished with our fetchemployee component.
Now open addemployee.component.ts file and put the following code into it:
This component will be used for both adding and editing employee data. Since we are using a form model along with client-side validation to Add and Edit customer data we have imported classes from
@angular/forms
. The code to create the form has been put in the constructor so that the form will be displayed as the page loads.
This component will handle both Add and Edit requests. So how will the system differentiate between both requests? The answer is routing. We need to define two different route parameters, one for adding employee records and another for editing employee record, which will be defined in the app.shared.module.ts file shortly.
We have declared the variable title to show on the top of the page and the variable id to store the employee id passed as the parameter in the case of an edit request. To read the employee id from the URL, we will use ActivatedRoute.snapshot inside the constructor and set the value of the variable id.
Inside
ngOnInit
, we are performing two operations:- We are fetching the list of cities by calling the
getCityList
method from our service. We will bind the list of cities to a dropdown list in our HTML page. Since we are calling thegetCityList
method inngOnInit
, the dropdown list will be populated as the page loads. - We will check if the id is set, then we will change the title to "Edit," get the data for that id from our service, and populate the fields in our form. The value read from the database will be returned as JSON and have all the same properties as we declared in our FormBuilder. Hence, we use the
setValue
method to populate our form.
The
save
method will be called by clicking on the "Save" button of our form. Based on whether it is an Add operation or an Edit operation it will call the corresponding method from our service and then, upon successful redirection, back to the fetch-employee component.
In the last one, we have also defined getter functions for the control names of our form to enable client-side validation.
Open the addemployee.component.html file and put the following code into it:
Here you can observe that we have the attribute
[formGroup]="employeeForm"
, which is our defined form group name in the addemployee.component.ts file. "(ngSubmit)="save()"
will invoke our save method upon form submission.
Also, every input control has the attribute
formControlName="xyz"
, this is used to bind FormControl
to our HTML. We have also defined error messages for the client-side validation check and they will be invoked upon form submission only.
For binding the dropdown list, we are using the
cityList
property that we have populated from the tblCities table by calling the getCityList
method from our service inside the ngOnInit
method of the addemployee.component.ts file.Defining Route and Navigation Menu for Our Application
Open /app/app.shared.module.ts file and put the following code into it.
Here we have also imported all our components and defined the route for our application as below:
- home - will redirect to the
home
component. - fetch-employee - to display all employee data using the
fetchemployee
component. - register-employee - to add a new employee record using the
createemployee
component. - employee/edit/:id - to edit an existing employee record using the
createemployee
component.
One last thing is to define the navigation menu for our application. Open the /app/components/navmenu/navmenu.component.html file and put the following code into it:
And that's it. We have created our first ASP.NET Core application using Angular 5 and Entity Framework Core's database first approach.
Execution Demo
Press F5 to launch the application.
A web page will open as shown in the image below. Notice the URL showing the route for our home component. And a navigation menu on the left showing the navigation link for the Home and Fetch Employee pages.
Click on Fetch Employee in the navigation menu. It will redirect you to the fetch employee component and display all the employee data on the page.
Since we have not added any data, it is empty.
Click on CreateNew to navigate to the /register-employee page. Add a new Employee record as shown in the image below. You can see that the City field is a dropdown list, containing all the city names that we have inserted into the tblCities table.
If we miss the data in any field while creating an employee record, we will get a required field validation error message.
After inserting the data in all the fields, click on the "Save" button. The new employee record will be created and you will be redirected to the /fetch-employee page, displaying records of all the employees. Here, we can also see the action methods, Edit and Delete.
If we want to edit an existing employee record, click the Edit action link. It will open the Edit page as below where we can change the employee data. Notice that we have passed an employee id in the URL parameter.
Here we have changed the city of the employee Rahul from Hyderabad to Chennai. Click on "Save" to return to the fetch-employee page to see the updated changes as highlighted in the image below:
If we miss any fields while editing employee records, then the Edit view will also throw a required field validation error message as shown in the image below:
Now, we will perform a Delete operation on an employee named Swati whose Employee ID is 2. Click on the Delete action link which will open a JavaScript confirmation box asking for a confirmation to delete.
Once we click on the Delete button the employee with the name Swati will be removed from our record, and you can see the updated list of employees as shown below.
Conclusion
We have successfully created an ASP.NET Core application using Angular 5 and Entity Framework Core's database first approach with the help of Visual Studio 2017 and SQL Server 2012. We have used Angular forms to get data from the user and also bind the dropdown list to a database table using the Entity Framework.
No comments:
Post a Comment