English 中文(简体)
what is the best way to use Html.DropDownListFor when you have a list of objects?
原标题:

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

问题回答

暂无回答




相关问题
ASP.Net MVC - Handle Multiple Checkboxes

Ok, I have a role based permission system in place and would like admin s to be able to edit the permissions for each role. To do this I need to load lots of checkboxes, however I m struggling with ...

Entity Framework error when submitting empty fields

VS 2010 Beta 2, .NET 4. In my ASP.NET MVC 2 application, when I submit a form to an action method that accepts an object created by the entity framework, I get the following error: Exception Details:...

ASP.Net MVC Architecture - Location of ViewModels

We have a decent sized MVC project running well at the moment, i ve started to look at some re-factoring and I have a question. At the moment the Data Layer and Service Layer is stored in a seperate ...

ASP.Net MVC Go To Declaration On view

Is there anyway when in an MVC view to right click a function call and get to the declaration? Like you can do in the code behind/controllers

ASP.NET MVC 2 Beta - Default Model Binder

I m experiencing some different behavior after switching from ASP.NET MVC 1.0 to ASP.NET MVC 2 Beta. I checked the breaking changes but it s not clear where the issue lies. The problem has to do with ...

Asp.net MVC Dynamic Menu System

In our current Asp.net MVC application we have 2 menu systems one accross the top and one on the left hand side. Now we have a partial view that renders the menu, however is the only way for this too ...

热门标签