Reputation: 2781
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>
Upvotes: 0
Views: 3258
Reputation: 545
Maybe changing the return type of DropDownlist method:
public static IEnumerable<MultValoredItem> DropDownList()
{
var brandServices = new BrandService();
var brandsDTO = brandServices.GetAll().OrderBy(b => b.Name);
var brandsList = brandsDTO.Select(brand =>
new MultValoredItem
{
Value1 = brand.Id.ToString(),
Value2 = //Another value here
Text = brand.Name
});
return brandsList;
}
public class MultValoredItem
{
public int Value1 { get; set; }
public int Value2 { get; set; }
public string Text { get; set; }
}
<ul id="sch-brand-dropdown">
@foreach (var brand in Model.BrandList)
{
<li data-id1="@brand.Value1" data-id2="@brand.Value2">@brand.Text</li>
}
</ul>
Upvotes: 1
Reputation: 117
Perhaps it is not the best option but you can use either a repeater. Or instead use the literal directly with HTML code.
class :
public class Brand
{
[Key]
public int Id { get; set; }
public int IdCATEGORY { get; set; }
public string Name { get; set; }
}
repeater :
<asp:Repeater ID="Repeater" runat="server" >
<ItemTemplate>
<li data-id="<%# Eval("id") %>" data-id2="<%# Eval("idCATEGORY") %>"><%# Eval("text") %></li>
</ItemTemplate>
</asp:Repeater>
use repeater in page_load:
Repeater.DataSource = data_Brand;
Repeater.DataBind();
Or literal -> then in c# create full string of <ul><li>
by for? foreach? while? and sen to literal inside page.
Upvotes: -1
Reputation: 4251
Modify your model like
[Table("Brands")]
public class Brand
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int Value1 { get; set; }
public int Value2 { get; set; }
}
and use
<ul id="sch-brand-dropdown">
@foreach (var brand in Model.BrandList)
{
<li data-id="@brand.Value1" data-id2="@brand.Value2">@brand.Text</li>
}
</ul>
Upvotes: 3