I need to build a select list with to values like this:
<ul id="sch-brand-dropdown">
<li data-id="1" data-id2="11">text1</li>
<li data-id="2" data-id2="12">text2</li>
<li data-id="3" data-id2="13">text3</li>
</ul>
For one value I use the following method:
public static IEnumerable<SelectListItem> DropDownList()
{
var brandServices = new BrandService();
var brandsDTO = brandServices.GetAll().OrderBy(b => b.Name);
var brandsList = brandsDTO.Select(brand =>
new SelectListItem
{
Value = brand.Id.ToString(),
Text = brand.Name
});
return brandsList;
}
Based on the Brand Model:
[Table("Brands")]
public class Brand
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
That I present in the view like this:
<ul id="sch-brand-dropdown">
@foreach (var brand in Model.BrandList)
{
<li data-id="@brand.Value">@brand.Text</li>
}
</ul>
IEnumerable<SelectListItem>(thats for use by the@Html.DropDownListFor()method) Just create your own class with 3 properties forID1,ID2andText