1 00:00:00,810 --> 00:00:09,440 Hey guys welcome to another exciting video in our journey with a speed that MVC web development. 2 00:00:09,450 --> 00:00:15,460 Today we will be looking into adding validations to our farms. 3 00:00:15,600 --> 00:00:22,770 Now validations are very important because when we are accepting input from a user trying to storage 4 00:00:22,770 --> 00:00:30,690 in our database we want to ensure that whatever we're passing with the database meets certain rules 5 00:00:30,930 --> 00:00:39,240 at the very least a rule can be like you expect a text value for text field or the expected number value 6 00:00:39,240 --> 00:00:43,630 for a number of fields or your text value should be at a certain length. 7 00:00:43,650 --> 00:00:51,600 Those kinds of things those are validations that you want to ensure we do from the client side meaning 8 00:00:51,600 --> 00:00:58,170 we don't want to try and hit the database with invalid data before we know if this data is admissible 9 00:00:58,170 --> 00:00:58,620 or not. 10 00:00:59,070 --> 00:01:06,750 So we want to add certain rules to our forms such that before the user is able to actually submit this 11 00:01:06,750 --> 00:01:15,120 information we can ensure that everything matches a certain standard to kick things off we'll take a 12 00:01:15,120 --> 00:01:19,020 look at our freight action in our course controller. 13 00:01:19,020 --> 00:01:27,630 As you can see I'm already in debug mode but we want to take a look at what happens when we submit data. 14 00:01:27,750 --> 00:01:33,610 So we have our create action which renders our form for free courses. 15 00:01:33,630 --> 00:01:36,220 I'm just going to bring that up quickly. 16 00:01:36,240 --> 00:01:43,050 We click on courses and then we click Create New which then renders our free view and then we ask the 17 00:01:43,050 --> 00:01:48,420 user what is the title of this horse and how many credits should it be. 18 00:01:48,420 --> 00:01:55,740 This is a number and I shouldn't be able to put in text those kinds of things matter when you want to 19 00:01:55,740 --> 00:01:58,170 validate the information coming back. 20 00:01:58,590 --> 00:02:06,090 If you take a look at what happens upon the post you see that it checks if model state is valid which 21 00:02:06,090 --> 00:02:13,500 means it is assessing all of the information that's it just got from the user and see is this information 22 00:02:13,530 --> 00:02:14,320 valid. 23 00:02:14,550 --> 00:02:24,030 What we're going to do in this video is set up our own set of validations so that this automatically 24 00:02:24,840 --> 00:02:29,370 assesses our rules and makes a decision based on that. 25 00:02:30,240 --> 00:02:35,010 But before we do that we're going to take a look at the database and we're going to take a look at what 26 00:02:35,010 --> 00:02:38,410 happens when we try to submit information. 27 00:02:38,520 --> 00:02:47,670 If we take a look at the course table which I already have over here and look at the designer to see 28 00:02:47,790 --> 00:02:57,810 that both title and credit columns are designed to low no values which in fairness is probably not the 29 00:02:57,810 --> 00:03:05,610 best thing to do because then it allows me to create a course with no title and no credits which doesn't 30 00:03:05,610 --> 00:03:07,930 really make sense. 31 00:03:08,040 --> 00:03:15,360 It hits my breakpoint on this continuum and it goes ahead and it actually creates that empty record 32 00:03:15,360 --> 00:03:22,440 with no value for a title and no value for credits and there is nothing there which is something that 33 00:03:22,440 --> 00:03:27,040 we really do not want a user to be able to do. 34 00:03:27,060 --> 00:03:35,970 So the first step would be to change the validation rules or change the data type rules on our database 35 00:03:36,240 --> 00:03:44,550 so we can head back over to visual studio bringing up our designer for our course database and we say 36 00:03:44,550 --> 00:03:47,250 we want to not alone knows. 37 00:03:47,280 --> 00:03:57,660 So we take those alone those and then we can pick up eight then we say OK it's obvious this is going 38 00:03:57,660 --> 00:04:03,910 to give us an error as you see on here because then we're sitting on our database to not accept those. 39 00:04:03,900 --> 00:04:06,640 Well we have no values that exist. 40 00:04:06,660 --> 00:04:08,770 So I distance to remove that no record. 41 00:04:09,240 --> 00:04:14,120 And since I have my code operation I just do it here since my code works. 42 00:04:14,130 --> 00:04:20,370 So I delete this record and I want to delete it. 43 00:04:20,370 --> 00:04:22,790 And once again we hit a very point. 44 00:04:22,980 --> 00:04:26,990 I'm just going to continue and there it is removed. 45 00:04:27,010 --> 00:04:29,820 So now we can retry our save. 46 00:04:30,880 --> 00:04:39,010 So I keep a bit of data again and then it is successful this time no less a strand had a course with 47 00:04:39,010 --> 00:04:41,290 no title and no credits once again. 48 00:04:41,320 --> 00:04:48,910 So we are going to leave these blank because we want to test if changing the know setting and the rules 49 00:04:48,910 --> 00:04:56,680 that govern our data types in our database will be effective enough in setting up validations here. 50 00:04:56,680 --> 00:05:05,290 So when we click creates we see that it actually hits an error it hits an exception saying that something 51 00:05:05,290 --> 00:05:08,470 happened while trying to execute this. 52 00:05:08,470 --> 00:05:16,570 And if you scroll down and read the details it says as a we cannot insert a no value into column type. 53 00:05:16,690 --> 00:05:21,110 So we see that some amount of validation is actually occurring here. 54 00:05:21,370 --> 00:05:28,630 But this is probably not the best because you don't want your application to actually pause like this 55 00:05:28,810 --> 00:05:35,820 when a user is using it or give these kinds of errors that a user may not necessarily understand. 56 00:05:36,130 --> 00:05:42,880 So effective continue then you see that the page precedes the load and very nasty looking error that 57 00:05:42,940 --> 00:05:49,420 the typical user may not necessarily know how to process and may not want to see at all. 58 00:05:50,020 --> 00:05:54,610 So we're going to have to modify this behavior a bit. 59 00:05:54,820 --> 00:06:00,760 So I'm going to quickly exit debug mode and then we will get to work 60 00:06:03,630 --> 00:06:09,280 know in order to actually add these validation rules. 61 00:06:09,290 --> 00:06:20,600 We have to use some C sharp classes and not some o p type implementations to have a very fluid and somewhat 62 00:06:20,600 --> 00:06:24,810 disjointed but unified solution and that sounds like a paradox. 63 00:06:25,010 --> 00:06:28,340 But as I go through it you appreciate what I'm saying. 64 00:06:28,760 --> 00:06:34,220 So the first thing we want to do is to take a look at our model. 65 00:06:34,220 --> 00:06:38,230 So we said that we want to start adding validations to our courses. 66 00:06:38,240 --> 00:06:46,400 So we draw a line into our models folder into our EDL mix diagram and then we drill down into our model 67 00:06:47,210 --> 00:06:59,060 T T and look at our course class we need to appreciate what is here and do recall that each field that 68 00:06:59,060 --> 00:07:04,890 is outlined here is synonymous with what is in the database. 69 00:07:04,910 --> 00:07:09,070 So the fact that we see here that credit is to a notable value. 70 00:07:09,150 --> 00:07:13,320 He's a we of Visit our database but we did not update our models. 71 00:07:13,360 --> 00:07:15,680 So that is the first step we need to take. 72 00:07:15,740 --> 00:07:25,100 So we vote for the mix diagram and then we right click in open space and click update from database 73 00:07:25,850 --> 00:07:32,090 he will to refresh tray tables and take finish. 74 00:07:34,590 --> 00:07:41,340 This then reloads our tables and since the last time we were here I added middle name and date of birth 75 00:07:41,340 --> 00:07:42,660 to our student club. 76 00:07:42,680 --> 00:07:50,680 Now do remember that each time we refresh or do meet a change of any sort to our diagram we need to 77 00:07:50,680 --> 00:07:58,930 build so you can either go to build and click the solution or hold down control shift and B and that 78 00:07:58,930 --> 00:08:02,460 will actually effect a build operation. 79 00:08:02,460 --> 00:08:07,270 So I'm going to do that control shift and the mind is already built. 80 00:08:07,300 --> 00:08:16,480 So when we go to our course that cease fire we will see that it is updated to show that it is no longer 81 00:08:16,560 --> 00:08:19,290 a noticeable value a test to have an INT. 82 00:08:19,540 --> 00:08:23,140 And the tests have a string and the test of an integer here. 83 00:08:23,140 --> 00:08:26,760 So you can see that this change has been made successfully. 84 00:08:26,920 --> 00:08:33,400 So that's a step one for validation making sure that our database reflects our ambitions. 85 00:08:33,400 --> 00:08:39,080 Step two to our validation is to create what we call meta data. 86 00:08:39,130 --> 00:08:46,480 Classes are already feeling the needs and means that we have to you call meta data which is pretty much 87 00:08:46,480 --> 00:08:47,920 data about data. 88 00:08:48,280 --> 00:08:57,130 So in this case we have data in the form of our model or our class or our database tables and the data 89 00:08:57,180 --> 00:09:00,000 about that would be descriptions that we want. 90 00:09:00,010 --> 00:09:05,800 And in this case the descriptions we want would be our validation rules. 91 00:09:05,890 --> 00:09:06,590 OK. 92 00:09:06,670 --> 00:09:14,770 So what we want to do at this juncture is to create some classes that would describe rules that will 93 00:09:14,830 --> 00:09:20,170 govern our values here. 94 00:09:20,310 --> 00:09:27,130 Now since the work we're able to do concerns models and the best place to go ahead and create the fires 95 00:09:27,130 --> 00:09:29,520 that we need would be in the models folder. 96 00:09:30,040 --> 00:09:36,430 But then if you look in the models folder you see that you already have a kind of variety at least if 97 00:09:36,430 --> 00:09:42,430 you created the project in the way that I did with the individual user codes set up at the start then 98 00:09:42,430 --> 00:09:46,330 you would have these view models already existing. 99 00:09:46,390 --> 00:09:53,080 So in order to not mix and maintain that separation of concerns I'm just going to create a new folder 100 00:09:53,650 --> 00:09:57,280 and I'm going to call it meta classes 101 00:10:01,450 --> 00:10:06,980 and then inside amateur classes then I will have those meta data classes that I alluded to just not 102 00:10:07,360 --> 00:10:15,520 the first one I create of course is one for courses so right click click Add create a class and I will 103 00:10:15,520 --> 00:10:29,750 call this one courses meta data God and then that will give me this empty class. 104 00:10:29,920 --> 00:10:30,450 No. 105 00:10:30,480 --> 00:10:34,580 My first objective is to meet this empty class Miro. 106 00:10:34,580 --> 00:10:44,750 My actual models so I can go back to my course that see us fail and find all of these properties that 107 00:10:44,750 --> 00:10:45,740 are there for me. 108 00:10:45,980 --> 00:10:49,140 I don't have to bring over I don't have to validate its course I did. 109 00:10:49,160 --> 00:10:55,220 Because upon creation or ADR any operation we don't need to validate what the value is here. 110 00:10:55,220 --> 00:10:59,110 What we may want to restrict any course title to a length of 50. 111 00:10:59,510 --> 00:11:06,320 And we would want people putting in 50 credits for any course that they're creating maybe the maximum 112 00:11:06,320 --> 00:11:08,910 number of critics that any course can BES 8. 113 00:11:08,930 --> 00:11:15,760 So we're just going to copy these two over to them in our meta data class 114 00:11:18,680 --> 00:11:23,510 and then proceed to add or validations before we move on. 115 00:11:23,540 --> 00:11:31,010 In order to add our validations or what we call data annotations we have to include our library that 116 00:11:31,010 --> 00:11:32,240 gives us that function. 117 00:11:32,240 --> 00:11:45,500 So we'll see using system lots components model dots data notations. 118 00:11:45,510 --> 00:11:53,390 Now you may not remember this but then when you see the pattern that I am able to use visual studio 119 00:11:53,390 --> 00:11:58,130 will actually recognize this pattern and see to you that hey this is illegal. 120 00:11:58,130 --> 00:12:03,140 You have to include this library to use this and then they would proceed to suggest that you include 121 00:12:03,140 --> 00:12:04,880 that library. 122 00:12:04,880 --> 00:12:11,030 So like I said the first thing I want to do is include a string length of 50. 123 00:12:11,030 --> 00:12:15,880 So literally I'm going to say string length and there and so the sense finishes it for us. 124 00:12:16,250 --> 00:12:24,890 And I'm going to see 50 0 if I remove this and notice that it has no come to life. 125 00:12:24,890 --> 00:12:29,880 Everybody else is sort of somewhat green notes but this one is actually bold white text. 126 00:12:30,020 --> 00:12:36,350 If I removed this then typing this would cause an error in Visual Studio and then they would suggest 127 00:12:36,350 --> 00:12:43,670 to me that I need that library so you can always start typing it before and then include the library 128 00:12:43,670 --> 00:12:45,080 retroactively. 129 00:12:45,080 --> 00:12:50,570 Or you can just include this library and then you have no problem moving forward. 130 00:12:51,740 --> 00:13:01,390 I also said that for my integer I want to restrict the range of values to me from one. 131 00:13:01,910 --> 00:13:09,530 So that means you can't of course with zero credits and it should never pass hate 132 00:13:13,860 --> 00:13:14,400 no. 133 00:13:14,400 --> 00:13:15,250 It is. 134 00:13:15,250 --> 00:13:22,080 Where are the off nodes that you can actually include these directly on your course. 135 00:13:22,080 --> 00:13:22,580 That's you. 136 00:13:22,600 --> 00:13:27,870 So you didn't have to necessarily go and create a whole new file a whole new folder and the whole new 137 00:13:27,870 --> 00:13:34,320 file and then start from scratch and look those there you could actually put them right here above title 138 00:13:35,250 --> 00:13:43,560 and both credits the downsides of this however is that each time the model gets generated or regenerated 139 00:13:43,560 --> 00:13:52,170 or dated or change and you build your project then you're actually going to have this cease fire as 140 00:13:52,170 --> 00:13:56,830 well with as every other one listed here refreshed. 141 00:13:56,850 --> 00:14:01,300 So any changes that you made to it will be discarded. 142 00:14:01,320 --> 00:14:05,730 So you really don't want to do that because that would lead to a lot of rework and you have to do this 143 00:14:05,730 --> 00:14:06,570 each time. 144 00:14:06,630 --> 00:14:15,150 So I am suggesting that you create this new file and then you associate it with that model using a partial 145 00:14:15,150 --> 00:14:18,420 class and that we you have that. 146 00:14:18,420 --> 00:14:24,510 As I said disjointed because they're actually different files but unified because they're making reference 147 00:14:24,510 --> 00:14:27,900 to each other so it doesn't matter what happens here. 148 00:14:27,900 --> 00:14:35,010 Even if the courses table ceases to exist these courses and metadata of course this table will not in 149 00:14:35,010 --> 00:14:39,230 any way encroach on the operation of your system. 150 00:14:39,240 --> 00:14:46,390 So next we're going to create actually another class and in C sharp you can actually use one cease fire 151 00:14:46,500 --> 00:14:48,540 as a whole as many classes. 152 00:14:48,600 --> 00:14:53,040 Some people say it's bad practice some people practice it it's up to you you can decide what you want 153 00:14:53,040 --> 00:14:53,400 to do. 154 00:14:53,730 --> 00:15:00,480 I'm just going to use this one C.S. Das called meta courses method data and I'm just going to create 155 00:15:00,570 --> 00:15:11,280 another public class but this one will be partial public partial class and I'm going to give it the 156 00:15:11,280 --> 00:15:15,660 same name as the model that I wanted to be associated with. 157 00:15:15,930 --> 00:15:21,450 So that's one of those great bull P tricks where I can create. 158 00:15:21,450 --> 00:15:28,050 Sorry the name of the class is course I can create a partial class and give it some properties and it 159 00:15:28,050 --> 00:15:31,960 will work in conjunction with our original class. 160 00:15:32,070 --> 00:15:40,890 What I wanted to do however is be associated with the meta data class so I'll use another data notation 161 00:15:40,990 --> 00:15:52,290 and I see a meta data type and this is some fun C C sharp that's going to happen here but you can study 162 00:15:52,290 --> 00:15:57,680 this pattern or just know how to do it when the time is right. 163 00:15:57,690 --> 00:16:07,740 So I want to associate it with courses meta data so at the end of this exercise you would have created 164 00:16:07,740 --> 00:16:17,790 a meta data class which would have data notations that will govern the validations of your fields and 165 00:16:17,790 --> 00:16:27,120 you would have associated with the class coming from your model just in a very indirect and or P like 166 00:16:27,210 --> 00:16:29,370 we know. 167 00:16:29,370 --> 00:16:37,170 One final change before we move on and this is very very very important and it's a common mistake that 168 00:16:37,170 --> 00:16:44,250 people would run into trying to maintain that separation of concerns but the partial class that you 169 00:16:44,250 --> 00:16:49,340 defined needs to be in the same namespace as the class it is. 170 00:16:49,340 --> 00:16:51,200 Well let's say pirates off. 171 00:16:51,360 --> 00:16:59,450 So if I go to courses and I look at the namespace I see that it is in school and management but models. 172 00:16:59,490 --> 00:17:07,190 If we look at where the namespace is for the partial classes see that is in modules thought classes. 173 00:17:07,650 --> 00:17:12,660 So each folder determines that namespace hierarchy. 174 00:17:12,660 --> 00:17:12,960 Right. 175 00:17:12,960 --> 00:17:19,860 So we're going from the project school management tool folder called models so school management to 176 00:17:19,860 --> 00:17:24,930 a folder called models of a folder called Method classes and that is its namespace. 177 00:17:25,440 --> 00:17:29,960 So what we want to do is remove the metal thousands from the namespace. 178 00:17:29,970 --> 00:17:39,480 We can leave it physically in the folder but the namespace associated with this folder and any other 179 00:17:39,480 --> 00:17:41,520 file in that folder. 180 00:17:41,520 --> 00:17:46,350 So by doing so we are putting or partial class on priority. 181 00:17:46,350 --> 00:17:54,660 The hierarchical sense with its original counterparts so we can go ahead and actually debug and see 182 00:17:54,660 --> 00:17:58,560 if our validation works hard again. 183 00:17:58,600 --> 00:18:08,980 So we will look at the courses section and just run some tests to see if our validations are working. 184 00:18:09,180 --> 00:18:16,170 So we go to create new and then firstly we're going to just click Create so they expectation is that 185 00:18:16,170 --> 00:18:26,490 once invalid data is being sent across it will re both our attempts to create the record and this be 186 00:18:26,520 --> 00:18:27,680 an error message. 187 00:18:27,690 --> 00:18:30,880 So we create and without filling anything in. 188 00:18:30,930 --> 00:18:36,730 We see that the pages of three loads with an error message the credits field is required. 189 00:18:36,750 --> 00:18:40,300 We can quickly review where that error message is coming from. 190 00:18:40,300 --> 00:18:48,330 I'm bringing up visual studio still in debug mode and I am just going to go over to our view for create 191 00:18:48,620 --> 00:18:56,220 and courses and then we can just take a look at what is happening in the credits section of the form. 192 00:18:56,640 --> 00:19:08,040 So remember we have that label which gives us the word credits and then we have the edit which gives 193 00:19:08,040 --> 00:19:14,550 us our textbooks but then read underneath that we actually have an issue him a helper that is sending 194 00:19:14,550 --> 00:19:18,280 back a validation message right. 195 00:19:18,570 --> 00:19:23,590 And we see that the bootstrap class takes that danger is being applied. 196 00:19:23,910 --> 00:19:32,310 So that is basically seeing that once I try to submit and I'm just going to go to my controller real 197 00:19:32,310 --> 00:19:37,300 quickly and put a breakpoint right at this decision. 198 00:19:37,300 --> 00:19:45,290 So he creates and it hits that breakpoint and then it evaluates if the model's state is valid and you 199 00:19:45,290 --> 00:19:52,140 see that this is evaluating the Falls unlike all the other times in the previous examples where it validated 200 00:19:52,230 --> 00:19:53,580 and which is true. 201 00:19:53,580 --> 00:19:59,790 So if this validates the true then that should happen the fact that it goes to falls means that it will 202 00:19:59,790 --> 00:20:08,080 just return the same view which is our create view and it will return whatever data was being submitted. 203 00:20:08,550 --> 00:20:10,720 We know that we didn't try to submit anything. 204 00:20:10,740 --> 00:20:18,610 So everything comes back as null and zero and then that is what triggers the display of or validation 205 00:20:18,660 --> 00:20:19,190 message. 206 00:20:19,500 --> 00:20:23,350 So if I put in a title and you can see that I was testing this thing. 207 00:20:23,520 --> 00:20:32,110 So I put in a title with clearly more than 50 ages and do recall that our validation rule for courses 208 00:20:32,320 --> 00:20:36,930 C is that we should only accept 50 or up to 50. 209 00:20:36,930 --> 00:20:44,850 So if I take create it hits it again and it says that this state is not valid so it returns a view with 210 00:20:44,850 --> 00:20:52,950 the data we see we get back our data alongside a displayed error message that we are exceeding or maximum. 211 00:20:52,960 --> 00:21:02,920 And if I see a 10 critic course once again recall that or validation rule said one 2 eight. 212 00:21:02,940 --> 00:21:08,910 So that means anything less than one and anything more than eight will trigger an error message. 213 00:21:08,910 --> 00:21:16,390 So if I create once again it's our breakpoints the model validity is false. 214 00:21:16,530 --> 00:21:24,720 So we continue and it will display an appropriate error message displaying to the user hey your data 215 00:21:24,720 --> 00:21:26,510 is wrong for this reason. 216 00:21:26,700 --> 00:21:33,240 And so this is a pretty cool feature that we can use to ensure that our users are staying within the 217 00:21:33,240 --> 00:21:37,710 confines of what we expect for our data. 218 00:21:37,710 --> 00:21:46,740 So I can create a legitimate course is the Mets and D.C. by Asian and that could be one credit course 219 00:21:46,860 --> 00:21:48,130 and then I create. 220 00:21:48,420 --> 00:21:55,080 And then we see it hits our breakpoints and this time everything is valid and because it is valid it 221 00:21:55,080 --> 00:22:01,500 will go ahead and edit and see the changes and then redirect to our listing of courses. 222 00:22:01,510 --> 00:22:05,600 So I press on to the expectation is fulfilled. 223 00:22:05,660 --> 00:22:08,210 There we have our brand new course. 224 00:22:08,220 --> 00:22:16,890 So that's it so you can go about setting validations on your fields no data annotations actually serve 225 00:22:16,890 --> 00:22:24,270 a greater purpose than just setting validation rules they can actually influence what is displayed for 226 00:22:24,270 --> 00:22:29,730 a farm or for a value in that display name for helper function. 227 00:22:29,730 --> 00:22:35,230 So I understand to jump over to your studio real quickly and show you what I mean. 228 00:22:35,310 --> 00:22:43,590 Do recall that we have the label for and then in our e Thales view we do have display name for. 229 00:22:43,650 --> 00:22:47,520 So you can actually use data annotation to influence these. 230 00:22:47,580 --> 00:22:55,290 The simple way to change this would be to erase it and rewrite what you once the downsides of that is 231 00:22:55,500 --> 00:23:03,210 if the field changes or something abodes the what should be displayed changes and you have it sets at 232 00:23:03,210 --> 00:23:08,360 different points off your project then you're going to have to manually go through and change it as 233 00:23:08,370 --> 00:23:15,270 all those points which can be tedious if you however continue to use the HMO function and then you use 234 00:23:15,270 --> 00:23:21,210 your data annotation then once you meet the change in the data annotation it will be made right across 235 00:23:21,210 --> 00:23:22,220 your application. 236 00:23:22,560 --> 00:23:31,170 So just as a quick example for or courses form we have title and credits just like the fields are in 237 00:23:31,170 --> 00:23:32,720 our database. 238 00:23:32,720 --> 00:23:34,580 All right title and credits. 239 00:23:34,620 --> 00:23:43,370 So as they appear in our database that's how they get created in our module title credits and that is 240 00:23:43,370 --> 00:23:51,510 what will be displayed in our label for on our page title and credits those details. 241 00:23:51,740 --> 00:23:55,070 This year title and credits. 242 00:23:55,100 --> 00:24:01,810 No it seems in a sense and it seems unimportant for our courses because we only have one word. 243 00:24:01,850 --> 00:24:08,570 Well what about the case of students where we are actually collecting first name last name enrollment 244 00:24:08,630 --> 00:24:14,570 age and other things so you can see here that the last name is appearing as it does in the database 245 00:24:14,570 --> 00:24:18,920 with no space no discernible space here for human readability. 246 00:24:18,920 --> 00:24:22,660 If we keep details we see everything above the students. 247 00:24:22,670 --> 00:24:27,330 And once again this is not very human readable or friendly. 248 00:24:27,350 --> 00:24:34,280 Now when we go to students online courses we don't have only one word but we have two words. 249 00:24:34,430 --> 00:24:41,510 So we click on students and we see that we have the last name first name and enrollment date with no 250 00:24:41,510 --> 00:24:46,870 discernible space in between these names and labels. 251 00:24:46,880 --> 00:24:48,650 And that is not very user friendly. 252 00:24:48,650 --> 00:24:49,940 That's not good. 253 00:24:50,000 --> 00:24:56,270 We want to make sure that our users look at our screen and feel comfortable absorbing and digesting 254 00:24:56,270 --> 00:24:57,150 our information. 255 00:24:57,150 --> 00:25:01,250 And if I go to details Izzy it is a very similar situation. 256 00:25:01,610 --> 00:25:09,530 So we're going to have to use some data annotation to clean this up and make our students much more 257 00:25:09,530 --> 00:25:12,350 user friendly to when we're reading the information. 258 00:25:12,380 --> 00:25:18,830 So back in Visual Studio we want to do is create two classes to associates with students. 259 00:25:18,830 --> 00:25:26,950 One for our metadata and the one for all where parts of us and to recall that I created this older here 260 00:25:27,020 --> 00:25:33,890 method classes or it's a visa but we subsequently need to change the name space so that everything flows 261 00:25:33,980 --> 00:25:34,780 smoothly. 262 00:25:34,930 --> 00:25:48,830 Someone's rightly had lots of class and we're creating students into data and then the easy way to go 263 00:25:48,830 --> 00:25:56,100 about doing this is just to copy all of the fields that are coming back from our original module in 264 00:25:56,100 --> 00:26:02,910 our database so we drill down into the EDL mix file we find students that see us and then we can copy 265 00:26:02,910 --> 00:26:08,370 across all of these know notes once again that these things are no levels. 266 00:26:08,370 --> 00:26:13,430 So for your own purpose you may go ahead and change a database of data your model. 267 00:26:13,440 --> 00:26:22,680 But I really just need to give the data annotations here for student meta data so I copy those across 268 00:26:22,710 --> 00:26:29,440 and we want to run some custom labels for all of these needs of these fields. 269 00:26:29,490 --> 00:26:39,000 The first one I'm going to do is last name and I'm going to see string length and I'm just going to 270 00:26:39,000 --> 00:26:44,920 set up a validation I don't want anybody entering a string length of more than 50 for our last name. 271 00:26:45,000 --> 00:26:52,140 So I'm setting up a validation right here and you see the arrows being displayed and Visual Studio is 272 00:26:52,140 --> 00:26:54,490 telling me I need to include this library in orbit. 273 00:26:54,510 --> 00:26:57,330 So I use this function. 274 00:26:57,330 --> 00:27:01,050 Secondly I want to see that the display 275 00:27:03,380 --> 00:27:10,130 needs to have a name and I'm going to set a custom string and you can always pay attention to the tool 276 00:27:10,190 --> 00:27:14,860 tips they're always guiding you as to what values can go in. 277 00:27:14,900 --> 00:27:20,990 We want a name which is what is going to be displayed in the UI. 278 00:27:20,990 --> 00:27:28,430 You can just read that description and ascertain what it is and I want to display in the UI a last space 279 00:27:28,700 --> 00:27:31,060 name all right. 280 00:27:31,120 --> 00:27:39,580 So we're moving away from last name as one perceivable word to two words and I can just reuse these 281 00:27:39,800 --> 00:27:42,770 annotations for the first name. 282 00:27:42,880 --> 00:27:50,170 I don't want more than 50 and first name just puts in space to make it look more beautiful enrollment 283 00:27:50,200 --> 00:27:50,820 date. 284 00:27:51,460 --> 00:27:56,960 Well I don't need to specify a length because this is a date. 285 00:27:57,880 --> 00:27:59,800 The data type is already set. 286 00:27:59,800 --> 00:28:02,640 It's an a low doesn't know anyway. 287 00:28:02,920 --> 00:28:16,420 And I want to display enrollments date with a space or better yet it seems the top of it date of Rome 288 00:28:16,540 --> 00:28:22,630 and so rewarding it just to show you that it's not a matter of or just putting a space in there you 289 00:28:22,630 --> 00:28:26,800 can labile to whatever you want on your form. 290 00:28:26,800 --> 00:28:27,460 All right. 291 00:28:28,030 --> 00:28:37,330 And let me just do the same for a middle name so middle name I'm limiting the number of characters sitting 292 00:28:37,330 --> 00:28:38,640 on middle space. 293 00:28:38,650 --> 00:28:46,500 Name and date of birth would have similar rules for enrollment dates and surplus IDs anyway so this 294 00:28:46,660 --> 00:28:58,450 date of birth and then the second step of this entire transformation process is to create a public plus 295 00:28:59,070 --> 00:29:12,880 a partial plus and call it student and associate the Metro data class with it. 296 00:29:13,240 --> 00:29:19,060 And you can look back at the previous school was understands and type it here into a data type and then 297 00:29:19,060 --> 00:29:29,140 in brackets we see it type of sorry type off and then we spell out that it should be off type student 298 00:29:29,200 --> 00:29:30,790 meta data. 299 00:29:30,940 --> 00:29:38,200 And as our final act which probably could have come first anyway we want to make sure that our namespace 300 00:29:38,500 --> 00:29:41,350 is the same as the namespace for our part. 301 00:29:41,380 --> 00:29:49,360 So Target class and then having done all of this we can go ahead and continue with our development or 302 00:29:49,360 --> 00:29:53,130 in debug and see if everything clicks. 303 00:29:53,200 --> 00:30:01,060 Now when our students page loads you can start seeing the differences already you see that discernible 304 00:30:01,060 --> 00:30:06,460 space between the last name first name date of enrolments. 305 00:30:06,460 --> 00:30:08,900 So the labels have changed. 306 00:30:08,950 --> 00:30:16,210 If we go to create news see that the labels for the form have changed the middle name dates of enrollment 307 00:30:16,210 --> 00:30:25,600 will ever include the dates of birth on the create page as yet details so everywhere and that is dynamically 308 00:30:25,720 --> 00:30:34,650 using that each time a helper that we pointed out is in details display name for or in the create we 309 00:30:34,660 --> 00:30:38,980 have label for or in our index. 310 00:30:38,980 --> 00:30:47,950 Next we have display name for everywhere that you have something like that by setting up or data annotations 311 00:30:48,190 --> 00:30:50,230 and setting up our method class. 312 00:30:50,280 --> 00:31:00,370 We will see the different labels that we set being displayed for our users and that is indeed and in 313 00:31:00,370 --> 00:31:06,830 fact how you go about customizing what is displayed without being too intrusive. 314 00:31:06,880 --> 00:31:12,190 And by leveraging the flexibility that this framework gives you.