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

asp.net-mvc, asp.net-mvc-3, html.dropdownlistfor, razor, selectlist

Solution

Extend contact

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

Problem

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?

Original source

Related problems