Alex Rivera | Logout

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

Asked 2012-04-05T14:42:14.570
13

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?

Edit
Report

2 Answers

25

Extend contact

public partial class Contact
{
   private string _nameFull;
   public string NameFull
   { 
      get{return FirstName + " " + LastName;}
   }
}
answered 2012-04-05T14:55:32.283
0

Add a property to your Contact class named FullName that simply returns FirstName + " " + LastName.

answered 2012-04-05T14:47:36.483

Your Answer