简体   繁体   English

如何通过在 Razor 页面中输入的字母过滤 @Html.DropDownListFor?

[英]How to filter @Html.DropDownListFor by letter entered in Razor Page?

This is the function in my Controller.cs这是我的 Controller.cs 中的功能

    public IActionResult Create()
    {
        //Create Nation List
        var region = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                        .Select(x => new RegionInfo(x.LCID));
        Regex regex = new Regex(@"^[A-Z]+$");
        List<Tuple<string, string>> countryList = (from x in region
                                                   select new Tuple<string, string>(x.ThreeLetterISORegionName, x.DisplayName))
                                    .Distinct()
                                    .OrderBy(x => x.Item2)
                                    .Where(x => regex.IsMatch(x.Item1))
                                    .ToList<Tuple<string, string>>();

        List<string> xxxCountryList = countryList
            .Select(x => new string(x.Item2 + " (" + x.Item1 + ")")).ToList();
        ViewBag.CountryTupleList = xxxCountryList;
        return View();
    }

And this is the code in my view.这是我认为的代码。

        <div class="form-group col-lg-8 col-md-8 col-xs-12">
                    <div class="ml-1">
                        @{                                
                            @Html.DropDownListFor(m => m.Nationality, new SelectList(ViewBag.CountryTupleList), "- Please Select -", new { @class = "form-control" })
                        }
                    </div>
                </div>

Currently the countries are being displayed in the dropdown list.当前,国家/地区显示在下拉列表中。 It's a bit tedious to scroll all the way down.一直向下滚动有点乏味。 Can anyone suggest me on how can i filter the counter?谁能建议我如何过滤计数器? ie enter letter "G" and only countries starting with letter G will be displayed!即输入字母“G”,只会显示以字母 G 开头的国家! Huge Thanks in advance!非常感谢提前!

I believe you are looking for Chosen as your solution我相信您正在寻找Chosen作为您的解决方案

https://julesjanssen.github.io/chosen/ https://julesjanssen.github.io/chosen/

It supports single and multi select :-)它支持单选和多选:-)

Kind regards亲切的问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM