Welcome back! In this post, we complete connecting the .NET Core Model View Controller (MVC) Web Application with the .NET Core Application Programmable Interface (API).
This took me a little longer than I had anticipated, so I broke it into a couple of GIT commits:
- Basic post to controller working for details – Git Code 1
- Post functionality done – Git Code 2
- Edit delete buttons added with partial functionality – Git Code 3
- Web Application fully hooked up to API – Git Code 4
To pick up where we left off, we were able to launch the application and see the pre-existing location. We could also click on the link and go to the details page. I had left the details link on the summary page and should have removed it because the location details controller method is no expecting an id.
So, this post’s activities are:
- I hooked up the location model to the details view (i.e. Details.cshtml). Please notice that I was adding a read-only attribute since the details page was static. This has changed. To allow the view to handle the edit and create functionality, I removed the read-only attribute and added an if/else statement. If the user is creating or updating a location, text boxes are used. If not, labels are. The if statement evaluates the ‘isEdit’ and ‘isNew’ properties on the model to make this decision.
- I put an ‘New’ button on the summary page so we can add locations.
- Clicking on ‘New’ opens the detail view and allows a user to enter new values.
- Clicking ‘Submit’ places the new values in the database
- The new record is displayed on the summary page
- We also had to convert our location model into the data model that the Web API can consume. As I understand it, generally ‘data’ stuff should go in the model. This has not been the case at every job I have had. For example, at one job the model was strictly a data object with properties only. I wanted to do things a little different this time. So, similar to initializing our web models in the constructor with the data model, I am going to add an export function that takes the current web model and creates its data model equivalent. I like to think of systems as castles with moats. The moat is the boundary with outside world. For our application, the LocationHttpClient.cs is the moat and the Web LocationModel.cs is where any ‘conversion’ takes place. With the export method added to the web location model, we serialize our object and submit it.