Hey guys.
Last time we left off we were creating the control for our courses and using scaffolding to generate
all of the views.
I left you with the challenge to go ahead and create the scene control or end views for our students
table.
Someone to do that quickly.
Just in case you probably didn't know what to do and I'll show you how simple it really is.
So rightly or until the stable he go to and controller select MVC 5 controller with use using entity
framework at then we select our model class.
In this case it would be students then we see that most of the information is already populated because
that's what we did last time.
So Visual Studio is keeping track of our activities thus far making it easier for us to just move along.
This time I will use song control our actions just to show you the slight difference in the core everything
else checks out.
So this week at at the end of the scaffolding exercise our new control is generated for students and
we see that it looks very similarity to what we're used to from our courses on controller.
Additionally if we look at our use table we will see that we have a new folder for students with the
same set of views that were present in courses.
Now if you take a closer look at your controller you see that you have a few new syntax words namely
async and A.
These are parts of the code that generated from when we said use async functions and pretty much async
handle with our code but the end that's a low.
Our methods still run asynchronously so this is in keeping with C sharps need for efficiency and loading
multiple threads to run in one application.
Outside of that we see that most of the code looks pretty much the same.
If we compare it with our courses controller or no in this video we want to focus particularly on making
modifications to our database as we go along.
We're using a database base first coding Hutter and which means that our database would have existed.
And then we build application on our own or our database is constantly evolving as we finish old and
muffles the functions needed for our application.
Now the challenge with this approach is that as we change our database we have to update our models
and also update our code.
Now I just generated this code.
All of our views and this control are based on the fields that were present in our database tables.
So if I look into the database
I'll see that I have three fields student I.T. last name first name and enrollment dates which means
that everything that would have been generated would have been generated around those particular fields.
So if I take a look at my control view I'll see that I have a form generated for last name first name
and enrollment dates.
Now what if I needed to put in the student's middle name.
I love that those the database initially.
And though I need to include it in what I have going on.
Someone to go into some views into my school's server object explore
and drill down into my database instance and find my database school management.
Evey table and make a modification to my student table so I right click and then click view designer
I will then proceed to add the middle name column to this table.
So I just come here in the middle name.
I wanted to be a far Shah 50.
Just like the other names.
And then I think of dates to commit that change.
Ones that obvious is done.
I no need to update my database model to be cognizant of the fact that there is a new column in this
table or in that particular class so if I bring up my DMX I'll see that the current students does not
have a middle name column.
So I right click in the empty space and I click Update model from database.
This will then give me a little wizard I can go to refresh tables and then finish at the end of that
process we see that we have middle name added here as a column.
Now that leaves me to have two of these all of ordinary to use to know that there is a middle name that
needs to be edited or handed in our creed.
So this control shift and V and make sure that everything builds successfully after unsuccessful builds.
Then I will go ahead and find my views that need to be edited.
So go Don't say my views folder for students and then first edits are create view.
So I need to pretty much replicates the structure that I have here for last name and first name and
since the form is uncertain that they're all very similar data types and these are constructs I can
just copy and paste this block of code and modify it so that we know you want middle.
So this is generating a label for the first name.
I just change this to middle name and you can see that the intelligence is picking up that there is
indeed an impact column in the model cause name.
So I just enter and then I will do this or edit for and also the validation message
I will also need to do this for my edit page.
I can just copy this considering that edit page looks very similarity to create page and I will just
create some space here and paste the scene code and once again our model on our edit page of his students.
And so it knows of the new property cause to make an additional step so that we can browse to this newly
created index pitch is to modify our layouts and added in our virus so just click on courses on through
the duplicate line and then I'll see students
and also we will change the controller name to students we can see this and then go ahead and e-book
once our window launches we will see that we have the link to our students space so we can click that
and be navigated to our list of students we know that we didn't add anything to the database.
And also you'll see that even though we added middle names to the model there is no middle name column
being set up here.
We did however modify the create it so we do have a middle name here on the page.
And if I go ahead and create and then not do n that enrollment it allows us to type in our free text.
So if you recall I was saying that there are certain situations where you may want to add custom controls
or text box and edit for has some limitation Salud wanted probably changed data type associated with
this input from text and set it to be time so we can at least get that big time picture or find a g
query or JavaScript caught in the control to put there.
We can do that at the later leads but for now I would just put generic dates
and then click Create Now if we take a look at this we see that our newly created record is here once
again on index we're not displaying the middle name.
However if we click edits we will see that we have a slight anomaly with our program.
We did not do enough to make sure that middle name was going to be populated upon trees.
As a result though we added the text box to the edit form.
There is no middle name displayed because there was no middle name committed to the death of these when
we clicked create.
So we still have a bit of work to do now going back to our code.
We can come out of the book mode for a second and see what additional work needs to be done to ensure
that our create function works.
If we take a look at the data we will also see the writing our table and if they view data and we see
that middle name was indeed and in fact sends across as a note.
That is because at the time of creation even though we put the text and the controls there or the middle
name we did not tell the control that each should indeed in fact look for a middle name when it is receiving
data from our view.
So if we take a look at our controller really quickly and we go to the create post function we see that
we have something here that is fine include and we have some parameters
do n students i.e. the last name first name enrollment date and no middle name know what is happening
in this include equals was followed by this string of fields coming from our student model is we are
looking for specific properties from this class that should be included when we compile or objects and
see if changes.
So if I made the modification here that we should take students any last name first name enrollment
it and tack on middle
name and this part is case sensitive so please be sure to write in the same way that the property appears
in the class and for the edits we can also go ahead and include the middle name and go into debug mode
and that tend to add another students then you should no longer have this problem.
So let us try knowing debug modem with attempts to add another then I click creates and then we have
a second student's record.
Now if I look at the edits then you will see our value coming back unlike our first one where there
was no value committed to the data.
Now if we look at the details page to see all of what we need to see about the students details really
and truly should list every single thing above the students here.
Note there is no middle name so we still have a bit of work to do to get literally into up in our D
fields page.
So I just quickly go over the details and we're still in debug mode find the details view and we can
see here that details page looks a bit differently from the other pages it is predominantly using the
issue demo helper function for Display display name for and display for.
So we can just take this and replicate this kind of logic and insert middle layer.
So I just put middle name and middle name C and then refresh or page and there we go.
Last name first name and our new additions to the family middle name.
Also we have enrollment dates there are things you can do is that the goal is to make our dates more
attractive possibly easier way to have accommodated all of these changes to our database and our subsequent
control and view code would have been to regenerates our controller and views based on the changes in
our model.
The danger to this however is that any changes you would have made would have been discarded and replaced
with the generic code.
So be very careful when you are going to make changes to your database and be cognizant of the fact
that you probably want to do it manually so maintain whatever code has preceded this gene.
Anyhow.
Thank you for tuning in and I do hope you found value this lesson.
See you next time.