The above example shows the cascading dropdowns. I've seen lots of different ways of populating a dropdownlistfor, I would like to do it via a View-Model. I'm really not sure which is the best way. Some examples seem to show just a list, some a
list of selectlistitems and others just a selectlist. Could someone please tell me the difference and what is the 'better' way to do it... I know it often depends upon the application.

I would also like to be able to preselect a Make if I wish, I've seen some examples where you include the ID in various ways, and others by tagging it on to the end of the select list. None of them seem to work.

Also the Model shouldn't really show unless the make has been selected. Would I do this via CSS or another method?

Finally I've put the "Please Select" at the end of the make, so it shows. This doesn't seem like a great way of doing this. Could you please point me in the right direction?

Thank you for your time and I'm sorry for so many stupid noobish questions!

Re: Selected Value on DropDownListFor

Normally, first dropdownlist will be populated from ViewModel. Second ddl will be populated through ajax post by passing the selected value. This is the efficient way of doing. Here you are minimizing the server loading data on view model without loading
the Models.In ajax post you are getting the data you required by passing the selected MakeId.

Re: Selected Value on DropDownListFor

Good morning Jay, I'd be glad to help you wrap your head around DDLFor. This is going to be a long post, bear with me...

Like any other helper with the "For" suffix at the end (TextBoxFor, CheckBoxFor, DisplayFor, etc), we use it to pair up a property of our View's model to the input element. The DropDownListFor is no exception. The basics of DDLFor is that you assign it
to a property of your model, and then add a List of items. If the model-property has a value, it will try to find a matching value in your List. If the model-property does not have a value, or doesn't match a value of the List, then the DropDownList will
display the first entry in the List (this is typical behavior when creating a record).

Let's say we have a model named "Car" and this model includes a property named "Make" (as in the make of a car/truck). We also have a List of makes (Acura, Chevy, Honda, Jaguar, Nissan, Toyota, etc) that we'll name 'carMakesList'.

This gives our DDL element the name "Make". When we post our form, our receiving action should have the "Car" model as a parameter, and any input elements mapped to our model (such as the DDL above) will automatically be associated to the 'Car' model. Here's a short example of what an "Create" view might look like:

[HttpPost]public ActionResult CreateNewCar(Car carInfo){ // carInfo takes the data from our form, which is declared as an instance of "Car" model db.Cars.Add(carInfo); db.SaveChanges();

return RedirectToAction("Index");}

Jamie Brown

Could someone please tell me the difference and what is the 'better' way to do it... I know it often depends upon the application.

"better" is relative to your application, as you stated. Sometimes you may be able to create a list from a database table

var myList = db.SomeTable.ToList();

Sometimes you need to manually create your list, as I did with the 'makesList' above. Personally, I enjoy using ViewBag objects when I can -- it saves creating additional view-models if the list is the only extra info I need for the View.

Jamie Brown

I would like to do it via a View-Model.

A ViewModel is a model that contains multiple models. A ViewModel allows you to have more than one strongly typed model in your View. Going back to our example with the Cars View, let's say instead of using a ViewBag to populated our DDL, we want to use a list of strongly typed objects instead.

The View references our ViewModel (note: the namespace is "ViewModels" as that is where I've saved our view model). We create the dropdownlistFor using the viewmodel's CarList property to create the List (and we assume that CarList has a column named "Make" that we set as both the value and text of our DDL). Lastly, we've flavored our DDL with some "select car" text as the default selected option. Now you'll notice that when we name the DDLFor, we've given it model.CarInfo.Make -- CarInfo is a property of our view model, and Make is a property of the "Car" model (named "CarInfo" in our view model).

Jamie Brown

I would also like to be able to preselect a Make

You can pre-select a DDL object in a few ways. Here's a simple example of creating a Select list with the default value "Honda". This would be acceptable for "Create" views when model.Make is "empty/null" (create model properties are empty/null by default).

We've purposefully made both the value and text "Make" because our model's Make property is expecting the name (string) of the Make. If we changed our original Car model to have the property "Make" be of type "int" (to represent the Id of the makeList), then we'd write it as

Value is the value that will be set to the input element when you submit the form. Text is what displays in the View to the user. So, if you have a list of objects with multiple properties, you should declare which property is the value and which is the text.

Jamie Brown

Also the Model shouldn't really show unless the make has been selected. Would I do this via CSS or another method?

You could hide your 2nd DDL (let's call it 'modelDropDown') with jquery. Then display it when you've selected something from your make dropdown list (named 'makeDropDown')

I hope you don't mind but I have a few questions? I have previously tried googling most of these but not really been able to find a satisfactory answer, and MSDN isn't what it used to be. Unless the information is now elsewhere.

I noticed that you added the selectlist into the view as in the below:

Other examples have put it in the controller and others in the view-model as in mine. I'm sure it depends upon the application, but I didn't know if there was some form of best practice as where to put it? Also some examples have used the selectlistitem
and some have used the selectlist.

I'm not new to .NET but my existing skillset is quite out of date now, mainly winforms with original ado.net alongside classic ASP, jquery, etc. It took me years to figure out where to put something and why, but I don't really have that time anymore....
:(

Re: Selected Value on DropDownListFor

Re: Selected Value on DropDownListFor

Thanks for the help guys. I just wrote a massive post and it says "I replied to a forum that needs the moderator to approve it". But it lets me post now? Hoping the moderator will approve the post, as it took ages!

Re: Selected Value on DropDownListFor

Jamie Brown

I noticed that you added the selectlist into the view as in the below:

Because I used a ViewBag, I had to explicitly create the SelectList. DDL does not like taking a dynamic object (such as ViewBag) that has no way of knowing what it is prior to processing. If you took the SelectList out, the View would give an error (at
least in visual studio).

Aside from that reason, SelectList does give the ability to specify what column should be your Value (passed to form), and what column should be your Text (shown in View). It also lets you explicitly state the selected value if you DON'T use the DDLFor