Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to contextually filter the content

I can create a drop-down list with many fields, but I would like to additionally filter results from the drop-down list by typing from the keyboard

What I have tried:

//Class Repository ninject
public SelectList  ChooseTypicalGate()
        {

            var valueTypical = (from c in db.GateSize
                               where c.IsActive == true && c.SizeIsTypical == true
                               orderby c.GateHeight ascending, c.GateWidth ascending
                               select new SelectListItem
                               {
                                   Text = "Hei. " + c.GateHeight + " - " + "Wid. " + c.GateWidth + " " + c.PanelType.NameanelType + " " + c.GatePrice + " EU",
                                   Value = c.IdGateSize.ToString()
                               });
            var value = new SelectList(valueTypical, "Value", "Text");

            return value;

        }

// Controller

 public ActionResult GateValuation()
        {



            ViewBag.ListTypicalGates = _repo.ChooseTypicalGate();   


            return View();

        }

//View

 <div class="row">
                                <div class="form-group  has-success">
                                    @Html.LabelFor(model => model.IdGateSize, "Gate Typical", htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div>
                                        @Html.DropDownListFor(x => x.IdGateSize, (SelectList)ViewBag.ListTypicalGates, "Please choose a gate ", new { @class = "form-control", @style = "min-width: 500px;" })
                                        @Html.ValidationMessageFor(model => model.IdGateSize, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
Posted
Updated 10-Mar-20 1:43am

1 solution

If you want to modify the appearance or behaviour of the drop-down list, you're going to need a JavaScript plugin.

Try Select2[^] - it will add a search box to filter your drop-down list.
Razor
@Html.DropDownListFor(x => x.IdGateSize, (SelectList)ViewBag.ListTypicalGates, "Please choose a gate ", new { @class = "js-select2", @style = "min-width: 500px;" })

<link href="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/js/select2.min.js"></script>

<script>
$(function(){
    $(".js-select2").select2();
});
</script>
There are lots of options to let you customise the appearance and behaviour.

Since you're using Bootstrap, you might also want to apply the Bootstrap theme:
GitHub - ttskch/select2-bootstrap4-theme: Select2 v4 theme for Bootstrap4[^]
 
Share this answer
 
Comments
Przemysław Szkaradek 10-Mar-20 9:47am    
"GitHub - ttskch / select2-bootstrap4-theme: Motyw Select2 v4 dla Bootstrap4 " -
do you have an example for bootstrap 3 ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900