views:

210

answers:

1

In my view model if have a:

  List<Car>

where car has an Id and a name. I want to create a dropdown box using

Html.DropDownListFor()

what is the best way to hook this up as i want to have the value of the item be the Id and the display to be the Name of the Car.

+1  A: 

What's in your view model,

to display the lis you want you would use

<%= Html.DropDownList("DropDownName", new SelectList(yourListOfCar, "Id", "Name"))%>

so if you want to use DropDownListFor, you would use is like this

<%= Html.DropDownList(model => model.IdCar, new SelectList(yourListOfCar, "Id", "UserName"))%>

where model is your view model

moi_meme