HelloWorld1
HelloWorld1

Reputation: 14108

Display two properties in the dropdownlist

Database table

Language

<%: Html.DropDownList("SprakID", new SelectList(ViewData["Sprak"] as IEnumerable, "languageID", "language", Model.languageID))%>

Goal:
Display the name of the language and what languageID in the DropDownList.

Problem:
Don't know how to display both language and languageID in the dropdownlist?

Upvotes: 2

Views: 2054

Answers (2)

David Ruttka
David Ruttka

Reputation: 14409

Add a property to your Language class, for example

 public string DataTextFieldLabel
    {
        get
        {
            return string.Format("{0} ({1})", language, languageId);
        }
    }

Now use it for the dataTextField

<%: Html.DropDownList("SprakID", new SelectList(ViewData["Sprak"] as IEnumerable, "languageID", "DataTextFieldLabel", Model.languageID))%>

Upvotes: 5

khr055
khr055

Reputation: 29032

DropDownLists only have DataValueField and DataTextField properties, so one thing I can think of is to make a read-only property that combines both the name of the language and the ID, formatted the way you want it, and have the DropDownList use that for the DataTextField.

Upvotes: 2

Related Questions