How to add icon in SelectListItem?

Dondon510 261 Reputation points
2022-08-22T00:38:34.137+00:00

How to add icon in SelectListItem? in my use case below, I want to add a 'home' icon if the parentPid == 0

                            while (dr.Read())  
                            {  
                                long parentPid = dr.GetInt64(dr.GetOrdinal("parent_organizations_pid"));  
  
                                string text = dr.GetString(dr.GetOrdinal("organization_name"));  
  
                                if (parentPid == 0)  
                                {  
                                    text = "data-icon:'home' " + text;  
                                }  
  
                                list.Add(new SelectListItem() { Value = dr.GetInt64(dr.GetOrdinal("pid")).ToString(), Text = text });  
                            }  
Developer technologies | ASP.NET | ASP.NET Core
{count} votes

2 answers

Sort by: Most helpful
  1. Dondon510 261 Reputation points
    2022-08-22T09:38:12.98+00:00

    Yes, I store the data in a table and populate the selectitem using codes below:

                                  while (dr.Read())  
                                 {  
                                     long parentPid = dr.GetInt64(dr.GetOrdinal("parent_organizations_pid"));  
          
                                     string text = dr.GetString(dr.GetOrdinal("organization_name"));  
          
                                     if (parentPid == 0)     // I expect to add an icon if the parentPid == 0  
                                     {  
                                         text = "data-icon:'home' " + text;  
                                     }  
          
                                     list.Add(new SelectListItem() { Value = dr.GetInt64(dr.GetOrdinal("pid")).ToString(), Text = text });  
                                 }  
    

    and in my .cshtml

                   <div class="content-header-left col-4 mb-2">  
                        <label class="form-label fw-bold">Organization</label>  
                        <div class="input-group input-group-merge">  
                            @Html.DropDownListFor(x => x.organizationsPid, Model.organizations, new { @id="organizations", @class= "form-select" })  
                        </div>  
                        <span asp-validation-for="organizations" class="text-danger"></span>  
                    </div>  
    

    I expect to have an icon for parentPid == 0


  2. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2022-08-22T17:07:48.787+00:00

    the Html.DropDownListFor() generates a <select> with <option>'s. the <option> tag does not support icons. there is limited support for CSS, but Html.DropDownListFor() does not support the class element for options anyway.

    typically javascript is used to implement a <select> with icons. select2 is commonly used library:

    https://select2.org/

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.