1 00:00:00,760 --> 00:00:06,480 We just added an action method to process form post for the Entries/Add path. 2 00:00:06,480 --> 00:00:09,980 Now we're ready to capture the request post data. 3 00:00:09,980 --> 00:00:14,169 Make sure that you've got a breakpoint in the AddPost action method like you 4 00:00:14,169 --> 00:00:14,762 see here. 5 00:00:14,762 --> 00:00:17,827 Press F5 to start the app. 6 00:00:17,827 --> 00:00:20,173 Then browse to the Add Entry page. 7 00:00:22,415 --> 00:00:29,530 Enter 1/1/2016 for the date field value and click the Save button. 8 00:00:31,270 --> 00:00:33,030 And now we're at our breakpoint. 9 00:00:34,290 --> 00:00:37,705 Let's see if we can find the request form data in the Autos window. 10 00:00:37,705 --> 00:00:40,200 I'll make the window a little larger. 11 00:00:42,880 --> 00:00:46,880 If we expand this, and Request, 12 00:00:49,390 --> 00:00:53,130 we can see that it has a property named Form. 13 00:00:53,130 --> 00:00:56,650 And to the right in the value column, we can see our form data. 14 00:00:57,930 --> 00:01:00,060 Let's go ahead and stop the app. 15 00:01:00,060 --> 00:01:04,823 The Request Form property can be used to extract our form values like this. 16 00:01:04,823 --> 00:01:10,402 string date = Request.Form, 17 00:01:10,402 --> 00:01:17,640 a pair of brackets, and the string Date. 18 00:01:18,650 --> 00:01:22,904 This approach is similar to how we index into an array to get a reference to 19 00:01:22,904 --> 00:01:24,176 a specific element. 20 00:01:24,176 --> 00:01:28,977 But instead of using a number for the index, we use a string that 21 00:01:28,977 --> 00:01:34,200 matches the form field name we want to retrieve the value for. 22 00:01:34,200 --> 00:01:39,189 This line of code just extracts the date form field value. 23 00:01:39,189 --> 00:01:43,062 We'd need to repeat this line of code for each form field.. 24 00:01:45,922 --> 00:01:52,852 ActivityId, Duration, 25 00:01:52,852 --> 00:02:01,680 Intensity, Exclude and Notes. 26 00:02:01,680 --> 00:02:06,740 Repeating this line of code for each form field would get ugly real fast. 27 00:02:06,740 --> 00:02:11,920 Luckily, MVC provides us with a much better way of capturing our form data. 28 00:02:11,920 --> 00:02:16,920 We can simply define method parameters for each form field value we need to capture. 29 00:02:18,080 --> 00:02:21,110 string date, for the date field. 30 00:02:22,580 --> 00:02:27,440 string activityId, for the activity field. 31 00:02:28,820 --> 00:02:31,470 string duration, for the duration field. 32 00:02:33,900 --> 00:02:36,790 string intensity, for the intensity field. 33 00:02:38,250 --> 00:02:41,740 string exclude, for the exclude field. 34 00:02:41,740 --> 00:02:45,830 And lastly, string notes, for the notes field. 35 00:02:45,830 --> 00:02:48,980 Also, now that we've added parameters to our method, we 36 00:02:48,980 --> 00:02:53,817 can change the name of the method back to Add, and remove the ActionName attribute. 37 00:02:56,170 --> 00:02:58,910 This is possible because our method signature 38 00:02:58,910 --> 00:03:01,240 now differs from the other Add method. 39 00:03:01,240 --> 00:03:03,797 Let's run our app again and see how this works. 40 00:03:06,143 --> 00:03:10,570 I'll enter the values 1/1/2016, 41 00:03:10,570 --> 00:03:15,115 1 for Activity, 23 for Duration, Low for 42 00:03:15,115 --> 00:03:20,750 Intensity, and leave Exclude and Notes blank. 43 00:03:20,750 --> 00:03:22,890 Then click the Save button to post the form. 44 00:03:25,308 --> 00:03:28,590 Okay, here we are at our breakpoint again. 45 00:03:28,590 --> 00:03:34,880 In the Autos window, we can see that the date, activity, duration and 46 00:03:34,880 --> 00:03:39,390 intensity parameters all contain the values that we provided in the form. 47 00:03:39,390 --> 00:03:41,080 How does this work? 48 00:03:41,080 --> 00:03:45,280 At a high level, when MVC has rendered a request to an action method, 49 00:03:45,280 --> 00:03:47,260 it looks at that method's parameters. 50 00:03:47,260 --> 00:03:50,765 And attempts to set their values by looking for request form fields, 51 00:03:50,765 --> 00:03:56,140 route data or request query string parameters with the same names. 52 00:03:56,140 --> 00:04:01,750 This process, which is an important part of MVC is called model binding. 53 00:04:01,750 --> 00:04:03,920 The part of MVC that's responsible for 54 00:04:03,920 --> 00:04:07,470 model binding is known as the model binder. 55 00:04:07,470 --> 00:04:10,780 If you'd like to learn more about how model binding works, 56 00:04:10,780 --> 00:04:14,420 see the teacher's notes for links to additional resources. 57 00:04:14,420 --> 00:04:16,760 Go ahead and press F5 to continue. 58 00:04:16,760 --> 00:04:22,270 Notice that when the form is re-displayed, we're losing our values that we provided. 59 00:04:22,270 --> 00:04:23,710 Why is this a problem? 60 00:04:23,710 --> 00:04:28,540 Well, sometimes it's necessary to re-display the form back to the user. 61 00:04:28,540 --> 00:04:32,970 For instance, if the user doesn't supply all the required values, 62 00:04:32,970 --> 00:04:36,190 we'd want to re-display the form along with a message 63 00:04:36,190 --> 00:04:39,410 asking the user to provide the missing values. 64 00:04:39,410 --> 00:04:41,395 This is called form validation. 65 00:04:41,395 --> 00:04:45,400 We'll see how to implement form validation later in this course. 66 00:04:49,988 --> 00:04:55,018 For now, let's use ViewBag to flow our parameter values back to our form. 67 00:04:55,018 --> 00:05:03,891 ViewBag.Date = date. 68 00:05:03,891 --> 00:05:11,289 ViewBag.ActivityId = activityId. 69 00:05:11,289 --> 00:05:17,887 ViewBag.Duration = duration. 70 00:05:17,887 --> 00:05:24,583 ViewBag.Intensity = intensity. 71 00:05:24,583 --> 00:05:31,686 ViewBag.Exclude = exclude. 72 00:05:31,686 --> 00:05:39,070 ViewBag.Notes = notes. 73 00:05:39,070 --> 00:05:43,451 Then, in our View, we need to set the input element value attributes to 74 00:05:43,451 --> 00:05:46,348 the corresponding ViewBag property values. 75 00:05:46,348 --> 00:05:55,000 value= @ViewBag.Date. 76 00:06:01,748 --> 00:06:07,961 Value= @ViewBag.ActivityId. 77 00:06:15,794 --> 00:06:21,542 Value= @ViewBag.Duration. 78 00:06:29,127 --> 00:06:35,590 Value= @ViewBag.Intensity. 79 00:06:41,790 --> 00:06:47,431 Value= @ViewBag.Exclude. 80 00:06:47,431 --> 00:06:50,330 And then, for the text area element, 81 00:06:53,404 --> 00:06:57,546 Just set its inner content to the ViewBag Notes property value. 82 00:07:07,599 --> 00:07:09,040 Let's test our app again. 83 00:07:10,970 --> 00:07:14,058 This time, let's provide a value for 84 00:07:14,058 --> 00:07:18,606 every field to ensure that they all persist correctly. 85 00:07:18,606 --> 00:07:22,797 Date, Activity, Duration, 86 00:07:22,797 --> 00:07:28,283 Intensity, Exclude, and then Notes. 87 00:07:31,322 --> 00:07:32,662 Then click Save. 88 00:07:40,651 --> 00:07:42,380 Here's our breakpoint again. 89 00:07:42,380 --> 00:07:46,961 Press F5 to continue, and here's all of our values. 90 00:07:49,190 --> 00:07:50,870 Go ahead and stop the app. 91 00:07:52,130 --> 00:07:54,770 If you're using GitHub, let's commit our changes. 92 00:07:55,860 --> 00:08:00,654 Enter a commit message of Updated the Add 93 00:08:00,654 --> 00:08:05,025 action method to capture POST data, 94 00:08:05,025 --> 00:08:08,980 and click the Commit All button. 95 00:08:11,700 --> 00:08:14,710 As we were fixing the flow of values to our form, 96 00:08:14,710 --> 00:08:18,970 you were probably getting the feeling there's a better way to do this. 97 00:08:18,970 --> 00:08:21,380 And, you're right, of course. 98 00:08:21,380 --> 00:08:24,040 But don't worry, by the end of this section, 99 00:08:24,040 --> 00:08:26,930 we'll have all of the necessary pieces in place. 100 00:08:26,930 --> 00:08:30,250 So we don't have to do these kinds of workarounds. 101 00:08:30,250 --> 00:08:33,940 In the next video, we'll continue to improve our Add action method.