Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Let’s see how we can use MVC to work with drop down lists.
Follow Along
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/v3.1 -b working-with-drop-down-lists
DropDownList vs ListBox
The <select>
HTML element is flexible enough to render a list that only allows a single selection vs a list that allows multiple selections.
ASP.NET MVC distinguishes between these two approaches by giving them different names—“drop down list” and “list box”. Drop down lists can be rendered using the DropDownList
or DropDownListFor
methods and list boxes can be rendered using the ListBox
or ListBoxFor
methods. Just remember that these methods produce the same underlying element—<select>
.
For more information about the HTML <select>
element, see this MDN (Mozilla Developer Network) page.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select
-
0:00
[MUSIC]
-
0:04
In a previous section, we built a basic version of the ad entry form for
-
0:08
our Fitness Frog web app.
-
0:11
Now we'll focus on improving our form so that it's easier to use.
-
0:15
We'll do that by replacing our forms' text boxes
-
0:19
with controls that more closely match the type of data that they're associated with.
-
0:23
In the next two videos, we'll update the activity field to use a dropdown list,
-
0:28
which will restrict the user's input to a list of provided values.
-
0:33
Let's start by updating the activity field to use the dropdown list for
-
0:37
HTML helper method.
-
0:39
That's easy enough to do.
-
0:41
We just need to change the text box text here in this method call to DropDownList.
-
0:47
And almost immediately we can see the Visual Studio
-
0:50
is telling us there's a problem with our second parameter.
-
0:53
If we retype the comma after the first parameter Visual Studio
-
0:57
will display the parameter list for each of the available method overloads.
-
1:02
The second parameter for the first method overload is something named select list
-
1:07
of type IEnumerable select list item.
-
1:10
Select list item is a simple object that's used to represent an item
-
1:15
in a dropdown list.
-
1:16
We can define an array of select list item objects, here at the top of our view.
-
1:21
Var selectlistitems = new array then a set of curly braces.
-
1:29
Then new selectlistitem curly
-
1:35
braces then Value equals 1,
-
1:41
Text equals Item 1 and
-
1:45
Selected equals false.
-
1:50
Then a comma.
-
1:52
Then let's add the second item.
-
1:54
New SelectListItem.
-
1:59
Value equals 2 Text equals Item 2 and
-
2:07
Selected equals true.
-
2:14
And don't forget our ending semi colon.
-
2:17
This collection of selectListItem objects would render the following markup.
-
2:21
A select element containing an option element
-
2:27
with a value of 1 and content of Item 1.
-
2:32
Then a second option element with a value of 2.
-
2:38
This one would be selected with content, Item 2.
-
2:44
I'll select and comment out this markup by pressing control K and control C.
-
2:49
This comments out the markup using an eraser comment
-
2:52
which will prevent the markup from being sent to the client.
-
2:56
Let's update our call to the DropDownListFor method, and run our app.
-
3:04
Excellent, here's our activity field with a drop down list.
-
3:08
If we click on it,
-
3:09
we can see the two items that we added to our select list items array.
-
3:14
Here's something interesting.
-
3:15
We set the second item as the selected item, but
-
3:18
the first item was actually selected.
-
3:21
The reason for this is simple enough.
-
3:23
Even if it's not readily apparent, remember, we're using the strongly typed
-
3:28
view, and our activity field is bound to the model's ActivityId property.
-
3:35
On the initial page load, the selected item in the DropDownList will be set to
-
3:40
the item whose value matches the bound property value.
-
3:44
Regardless of the selected property values that we set up here.
-
3:52
In our controller, we're setting the value for the date property but
-
3:56
not the activity ID property.
-
3:59
That means that our property is getting the default value for
-
4:02
its underlying data type.
-
4:04
Right-click on Entry and select Go To Definition.
-
4:10
And scroll down to the ActivityID property.
-
4:21
The ActivityID property is of type int.
-
4:24
So our default value is zero.
-
4:30
We don't have an item in our SelectList whose value matches our property value.
-
4:35
So the first item in the list will be the selected item.
-
4:38
Let's stop the app.
-
4:44
If we set the Activity ID property value to 2.
-
4:47
Then the second item in the list will become the selected item.
-
4:51
Before we run the app, let's also update the post action method to do the same.
-
5:00
In an attempt to always force the selected item to the second item.
-
5:05
Let's also set a breakpoint on this line of code.
-
5:10
Go ahead and run the app.
-
5:14
Now, the second item is the selected item.
-
5:17
Select the first item in the list, remove the duration field value, and
-
5:22
save the form.
-
5:25
Here, we are in our post action method,
-
5:27
setting the entries activity id property value to 2.
-
5:31
Press F5 to continue and
-
5:33
we'll see another behavior that might be surprising forcing the activity id
-
5:38
property value to two in the action method has no effect on this like that item.
-
5:43
Item one is still selected.
-
5:46
This is happening because N.V.C.
-
5:48
gives priority to the model state values over the model property values.
-
5:53
Provided that there are model state values available.
-
5:56
On the initial page load the model state values collection is empty.
-
6:01
So the model property values are used instead.
-
6:04
On the second page load after our form post
-
6:08
the model state is populated with values.
-
6:10
So our model values are ignored when the view is re-rendered.
-
6:15
We saw in an earlier video how ModelState makes it possible to redisplay user values
-
6:19
even when they can't be converted to the data types defined in our model.
-
6:25
The behavior of our dropdown list selected item is another example
-
6:30
of NVC giving priority to the users provided values.
-
6:34
While any collection of select list item objects will work as the source of items
-
6:38
for the dropdown list for method,
-
6:40
MVC also provides a custom collection named select list.
-
6:45
Select list makes it easy to convert any collection
-
6:48
to a collection of select list item objects.
-
6:51
First, we need a collection of objects that will service our source data.
-
6:55
To keep things simple, I'll just use an array of anonymous objects.
-
6:59
Var items equals new array and then a set of curly braces.
-
7:06
Then new anonymous object Id equals 1.
-
7:11
Name equals Item 1,
-
7:15
then new anonymous object { id = 2 }.
-
7:22
Name = "Item 2".
-
7:26
And don't forget our ending semicolon.
-
7:30
Then we can instantiate a selectList object by calling its constructor and
-
7:35
passing in our source data.
-
7:37
Along with the names of our value and text fields.
-
7:40
Notice that there is a constructor overload that allows us to pass in
-
7:44
an object representing the selected value.
-
7:47
Internally the select list constructor will create a select list item object for
-
7:52
each item in our source data.
-
7:54
And for the item whose value matches are provided selected value
-
7:58
it's selected property will be set the true.
-
8:01
So attempting to set the selected value v of the select list constructor
-
8:06
will be as ineffective as it was with our prior approach.
-
8:10
Given that I'm not going to use that constructor overload.
-
8:14
Even though we've switched to using the select list custom collection.
-
8:18
We shouldn't notice a change in our page.
-
8:21
Save the view and refresh the page.
-
8:28
And everything looks and behaves as it did before.
-
8:34
After the break,
-
8:35
let's replace our temporary data with our actual available activities.
You need to sign up for Treehouse in order to download course files.
Sign up