Last Updated: February 25, 2016
·
1.802K
· iondrimba

Display custom dropdown label value with LINQ (Razor)

//In your .cshtml file

@{
    var newSource = YourModel.SomeCollection.Select(m => new SelectListItem
    {
        Value = m.YourValueField.ToString(),
        Text = string.Format("Custom Text {0}: {1} {2}", m.FirstTextField, m.SecondTextField, m.ThirdTextField)
    });
    SelectList finalSource = new SelectList(newSource, "Value ", "Text");
}

@Html.DropDownList("ControlName", finalSource)