How to combine two dataTextFields for SelectList Description in asp.net MVC 3 using @Html.DropDownListFor

user864675 picture user864675 · Apr 5, 2012 · Viewed 17.3k times · Source

I am returning some stored contacts to view for DropDownList and I am not able to include multiple dataTextFields on my SelectList.

Currently I have:

@Html.DropDownListFor(model => model.Account.AccountContacts, 
         new SelectList(ViewBag.DDContacts, "Contact.ContactID", "Contact.FirstName"),   
         new { @class = "select", style = "width: 100px;" })

I would like to have:

@Html.DropDownListFor(model => model.Account.AccountContacts, 
        new SelectList(ViewBag.DDContacts, "Contact.ContactID", "Contact.FullName"),         
        new { @class = "select", style = "width: 100px;" })

which combines both FirstName and LastName properties.

UPDATE: I am aware of extending contact property, I was curious was there a streamline way to accomplish this in the View or Controller. I tried the two solutions here to no avail. How can I combine two fields in a SelectList text description?

Answer

Antarr Byrd picture Antarr Byrd · Apr 5, 2012

Extend contact

public partial class Contact
{
   private string _nameFull;
   public string NameFull
   { 
      get{return FirstName + " " + LastName;}
   }
}