Question

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.

Was it helpful?

Solution

What's in your view model,

to display the list 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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top