views:

27

answers:

2

This is my DropDownList in ASP.Net:

<%: Html.DropDownListFor(x => x.CompanyUserFilterId, new SelectList(Model.CompanyUsers, "Id", "FirstName", Model.CompanyUserFilterId)) %>

I want to list all CompanyUsers in the DropDownList and show both their FirstName and LastName property. CompanyUsers is an IEnumerable property. The code above works and shows only FirstName property. Is it possible to show both?

+1  A: 

Instead of passing your complete customer data as the model, you will have to instead provide a projection of your data which contains the concatenated field.

You haven't provided your controller implementation but you probably want to do something like this:

var data = from c in DataContext select new CustomerData() { Id = c.Id, Name = c.FirstName + " " + c.LastName };

return View(data);

.. where CustomerData is a simple POCO with Id and Name properties on it. Then change your view to use IEnumerable<CustomerData> and your drop-down list code to use the 'Name' property from your projected model, instead of the FirstName property you had originally.

Hopefully you can tweak that to suit your own specific scenario.

elkdanger
A: 

I'll add that the MvcContrib's drop down lists is soo soo soo much better than the default one, I'd consider using that library if at all possible.

Wyatt Barnett