Display 2 dataTextField in a SelectList

4

Hello! I have a problem with making a selectlist display 2 values in textfield. I have the following SelectList that is stored in a viewbag:

ViewBag.Account = new SelectList(new AccountBusiness().GetList<Account>(Util.AuxiliaryMethods.BMPerRequestInstance).ToList(), "AccountId", "AccountNumber");

On the DOM it displays the AccountNumber in the Option text and the AccountId in its value. On the screen it displays as follows:

IneedittodisplayinsteadofjusttheAccountNumber,alsodisplayanotherfield,whichistheDescription,soIwouldgettheAccountNumber+Description,somethinglike"15252 - Standart", and in value it would still return the Id How can I make SelectList get two parameters?

    
asked by anonymous 28.08.2018 / 15:01

1 answer

2

Just populate your SelectList() with a List<SelectListItem> or compose another structurally object by supplying the values that will be used for the Value and Text properties.

var selectAccounts = new AccountBusiness()
                            .GetList<Account>(Util.AuxiliaryMethods.BMPerRequestInstance)
                            .Select(x => new { Value = x.AccountId, Text = string.Format("{0} - {1}", x.AccountNumber, x.Description})
                            .ToList();
ViewBag.Account = new SelectList(selectAccounts, "Value", "Text"));
    
28.08.2018 / 15:56