用组合框替换下拉列表(可编辑下拉列表)

问题描述 投票:0回答:1
c# asp.net-mvc asp.net-core
1个回答
0
投票

经典 asp .net core 中的组合框有其他选择吗?

我认为你可以使用 Select2.js:

<div class="col-md-10">
    <label> Account Number</label>
    <div class="input-group">
        <select id="mySelect2" class="form-control accountSelect">
            <option value="Select" disabled>Select</option>
            <option value="aaa">aaa</option>
            <option value="bbb">bbb</option>
            <option value="ccc">ccc</option>
            <option value="ddd">ddd</option>
        </select>
    </div>
</div>

@section Scripts
{
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
     <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#mySelect2").select2();
        })
    </script>
}

结果:

enter image description here

如果您的选择列表资源来自后端,您可以更改视图,如下所示:

索引.cshtml:

@model TestVM
<div class="col-md-10">
    <label> Account Number</label>
    <div class="input-group">
     
        <select id="mySelect2" asp-items="Model.Select" class="form-control"></select>
    </div>
</div>

型号:

public class TestVM
{
    public IEnumerable<SelectListItem> Select{ get; set; }     
}

控制器:

[HttpGet]
public async Task<IActionResult> Index()
{
    var model = new TestVM()
    {
        Select= new List<SelectListItem>() {
                    new SelectListItem() { Value = "1", Text = "aaa" },
                    new SelectListItem() { Value = "2", Text = "bbb" },
                    new SelectListItem() { Value = "3", Text = "ccc" },
                    new SelectListItem() { Value = "4", Text = "ddd" }
                    }
        
    };
    return View(model);
}
© www.soinside.com 2019 - 2024. All rights reserved.