Task 2: Adding Employers
ViewModels
- Create a new ViewModel called
AddEmployerViewModel
that has 2 properties:Name
andLocation
. For this application, an employer can only have one location. - Add validation to both properties in the ViewModel so that both properties are required. The depth of your validation is up to you.
- This class does not need a constructor.
Controllers
EmployerController
contains four relatively empty action methods. Take the following steps to handle traffic between the views and the model:
- Set up a private
JobDbContext
variable so you can perform CRUD operations on the database. Pass it into aEmployerController
constructor. - Complete
Index()
so that it passes all of theEmployer
objects in the database to the view. - Create an instance of
AddEmployerViewModel
inside of theAdd()
method and pass the instance into theView()
return method. - Add the appropriate code to
ProcessAddEmployerForm()
so that it will process form submissions and make sure that only validEmployer
objects are being saved to the database.- You want to add a new employer if the model is valid.
- Else redirect users back to the form
About()
takes an id as a parameter. It will create anEmployer
object by searching through the Employers table inDbContext
until it finds the provided id. It will pass the employer object to the view.- Consider using the
.Find()
method to search the database.
- Consider using the
Views
The starter code comes with 3 views in the Employer
subdirectory. Read through the code in each view. You may have to add models or make sure naming is consistent between the controller and the view. Make sure you are using your new AddEmployerViewModel
and its validation where necessary.
About
View: Use the Model to help populate the viewAdd
View: Use tag helpers to use theAddEmployerViewModel
âs validationIndex
View: Create a way to add a new employer on this View.
Model
We need to create a List
of Job
objects named Jobs
in the Employer
model. Make sure it has both a getter and a setter.
Adding a Job
One important feature of your application is a form to add a new job. Two action methods in JobController
, Add()
and ProcessAddJobForm()
, will work together to return the view that contains the form and handle form submission. In the Job
subdirectory in Views, you will find an Add.cshtml
file which contains the beginning of the form. Right now, the form only has one field for the jobâs name. As you work on the application, you will add more fields to this form to add employer and skill info.
Create a new ViewModel called
AddJobViewModel
. You will need properties for the jobâs name, the selected employerâs ID, and a list of all employers asSelectListItem
. Make sure that the name of a job is required.NoteThis is different from the given ViewModel,
JobDetailViewModel
.JobDetailViewModel
has properties for the selected employerâs info and the selected skillâs info.AddJobViewModel
will have properties for all of the employers and skills in the database once you complete task 3. We need both ViewModels for the application.Back in the
JobController
, find theAdd()
method.- This method needs to contain a list of Employer objects which it pulls from the Employers dbContext.
- This method needs to create an instance of the
AddJobViewModel
which is passed the list of employer objects. - Pass an instance of AddJobViewModel to the view.
In
Add.cshtml
view, add a new<div class="form-group">
element to the form. Add the appropriate<label>
and<input>
tags to the new<div>
element to create the form field to add employer information to the job. This field should be a dropdown menu with all of the employers in the database. In addition, add a link to the<div>
element to add new employers. This way, if a user doesnât see the employer they are looking for, they can easily click on the link and add a new employer to the database.Back in the
JobController
, renameProcessAddJobForm()
toAdd()
and add the[HttpPost]
attribute to designate this as your post handler.- This post handler needs to take in an instance of
AddJobViewModel
and make sure that any validation conditions you want to add are met before creating a new Job object and saving it to the database. - If model is valid, redirect to the
â/Jobsâ
.
- This post handler needs to take in an instance of
Create the One-to-Many Relationship
In the JobDbContext
, we need to add the following to the OnModelCreating()
method:
|
|
This will create the many-to-one relationship between Jobs
and Employers
.
Project Check:
- You should be able to add an employer.
- You should be able to add a job with your new employer.
- You should be able to create many new jobs for a single employer.
Database: Check your data by adding employers to jobs.
- You should see employers in the
Employers
table. - You should see Employer IDs in the
Jobs
table. - You should see jobs in the
Jobs
table.
Troubleshooting Tips
- If your database is not updating, try running a new migration followed by an update.
Test It with SQL
Before you move on, try out your application now to make sure it runs as expected. You should be able to create Employer objects and view them both in the browser and in your database.
- Open MySQL Workbench and make sure you have an
Employers
table and that it is empty. - Start up your application â donât forget to have your SQL server running â and go to the
Add
Jobs
view. - You wonât be able to add a job yet, but youâll see a link to
Add Employers
in the form. Click on it and proceed to check the functionality of the form that follows. - Be sure to test your validation requirements and error handling.
SQL Task 2
In queries.sql
under “Task 2â, write a query to list the names of the employers in St. Louis City.
If everything seems to work â that is, you are able to submit the form without any errors â but you donât see your employers in the list after submission, hereâs what you should check:
- Is there any data in the
Employers
table? Check by going to MySQL Workbench and looking for the employer data within your schema. - If thereâs data in the database, check that you are correctly querying for the list of all objects in the controller Are you calling for the proper list with
DbContext
? - Ensure youâre passing the list into the view.
You are ready to move onto Task 3 .