0

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>
6
  • what is the question? Commented Oct 23, 2015 at 12:09
  • Hi, the question is how can I build a select list with 2 values and 1 text Commented Oct 23, 2015 at 12:09
  • Dont use IEnumerable<SelectListItem> (thats for use by the @Html.DropDownListFor() method) Just create your own class with 3 properties for ID1, ID2 and Text Commented Oct 23, 2015 at 12:12
  • @StephenMuecke Hi, thanks! can you just give me a simple example? Commented Oct 23, 2015 at 12:15
  • @Patrick, The answers by Mikhail Neofitov and Júlio Murta should be helpful Commented Oct 23, 2015 at 12:19

3 Answers 3

3

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Mikhail! Thanks, don't get me wrong, I have set Júlio's answer as correct because it was the fastest and most complete one.
1

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>

2 Comments

Hi, thanks! Can I ask you please to put in your example the way I can apply it in the View?
Thanks again Júlio, I have set your answer as correct because it was the fastest and most complete one :)
-1

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.

1 Comment

Hi, thank you for your help but I prefer Júlio's answer becaus it's MVC based and more close to my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.