문제

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.

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top