Using Our Models5:11 with James Churchill
Let’s update our web app to take full advantage of the Entry data model.
To follow along commiting your changes to this course, you'll need to fork the aspnet-fitness-frog repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd aspnet-fitness-frog git checkout tags/v2.7 -b using-our-models
Is String a Value or Reference Type?
From a behavioral point of view, strings in .NET are value types. That being said, there are some distinct differences between how strings and other value types—like integers and floats—are stored in memory.
For an interesting discussion on those differences—and if those differences really matter—see this Visual Studio Magazine article by Peter Vogel.
See this MSDN page for more information on C# lambda expressions.
Let's update our web app to take full advantage of the entry data model, 0:00 starting with our Controller. 0:04 Instead of specifying a parameter for each request form field that we need to 0:06 capture, we can simply use a parameter of type entry named Entry. 0:10 How was this possible? 0:15 MVCs model binder will recognize that our parameter is an instance of a class or 0:17 reference type instead of a value type like string, int, double, bull, or 0:22 date time and attempt to bind the incoming form field values to its properties. 0:28 As long as the field names match the classes property names 0:34 the entry object's properties will contain the expected values. 0:38 Before we move on to updating our view, let's update each of our 0:42 add action methods to pass an instance of the entry data model to our view. 0:45 For the git version of our add method we need to instantiate a new model instance. 0:51 Var entry equals new Entry. 0:56 I'll go ahead and default the date property to today. 1:01 And pass the entry variable to the few method call. 1:10 For the post version of the add method, 1:18 we just need to pass the entry parameter to the view method call. 1:20 Let's turn our attention to our View. 1:27 First, we'll make our view strongly typed 1:30 by adding a model directive to the top of the View. 1:33 @model 1:35 Treehouse.FitnessFrog.Models.Entry. 1:37 By making our view strongly typed, 1:45 the entry model instance that is being passed into the controllers call to 1:47 the view method will now be available via the views model property. 1:52 We can also update our label, text box and 1:56 text area HTML helper methods to their strongly typed versions. 1:59 Starting with the date field label, we'll change the label method call 2:04 to label four and replace our date string with a => expression. 2:08 Remember, you can think of a => expression as an anonymous method. 2:13 The m here is a method parameter that is a reference to our views model property. 2:18 The => operator is the transition from the parameter list to the method body. 2:23 And m.Date is the property on our model 2:29 that we're returning from our anonymous method. 2:32 This approach gives us a strongly typed way 2:35 to specify the model property that we want to render a label for. 2:38 Replacing our date string literal with a => expression is a huge improvement. 2:42 Instead of having to just magically know what our field name is 2:47 Visual Studio is able to show us the list of available properties on our model. 2:51 This reduces the amount of typing we have to do and decreases the chance for errors. 2:55 We can also update our TextBox method call to it strongly typed version. 3:01 TextBoxFor it also takes a => expression as its first parameter and 3:05 we no longer need the second parameter, 3:10 Let's update our other fields by using the same copy and 3:17 paste trick that I did in the previous video. 3:20 This time I'll remove the property name and preceding dot before copying and 3:22 pasting to the other fields. 3:27 Now, we just need to go back and press dot at the end of the in variables to display 3:42 the list of available properties and select the one that we're looking for. 3:48 .ActivityId, and again .ActivityId. 3:53 .Duration and .Duration, 4:02 .Intensity and .Intensity, 4:11 And lastly, .Exclude and 4:18 .Exclude. 4:24 And now the notes field. 4:27 Change label to labelFor, and 4:29 provide a => expression to the m.Notes property. 4:32 And text area to TextAreaFor provide a => 4:44 expression again to the m.Notes property. 4:49 And remove the second parameter. 5:01 After the break, we'll make one more change to our controller and 5:05 test our changes. 5:09
You need to sign up for Treehouse in order to download course files.Sign up