简体   繁体   English

如何设置在 DropDownListFor 中选择的所有元素并添加像 checkBox 这样的图标

[英]How Set all element selected in DropDownListFor and add icon like checkedBox

am looking how to set all element selected in DropDownListFor in asp.net core我正在寻找如何在 asp.net 核心中设置 DropDownListFor 中选择的所有元素

@Html.DropDownListFor(m => m.SubmittedFloors,
new SelectList(Model.ProfileList, "Id", "Name",Model.FloorList),
"-- Please Select --",
new { @multiple = "multiple", @class = "form-control multiselect", @style = "height:220px" })

<script type="text/javascript">
    $(function () {
        $('.multiselect').multiselect({
            includeSelectAllOption: true
        });
    });
</script>

In my model builder:在我的 model 构建器中:

public class FloorBuilder{
public List<string> SubmittedFloors { get; set; }
public List<GenericType> FloorList { get; set; }
public class GenericType
{
public string Name { get; set; }
public string Id { get; set; }
public GenericType(string id,string name)
        {
            Id = id;
            Name = name;
        }

    }
Model.FloorList =FloorService.getFloors()

}

the data are showing in DropDownListFor correctly, but the elements not selected and if there is a way to add a boxcheck on next to every item.数据正确显示在 DropDownListFor 中,但未选择元素,以及是否有办法在每个项目旁边添加框检查。 1 thanks in advance 1提前致谢

You could use the bootstrap multiselect plugin to display the DropDownList with checkboxes.您可以使用引导多选插件来显示带有复选框的 DropDownList。

Sample code as below:示例代码如下:

public class FloorBuilder
{
    public List<string> SubmittedFloors { get; set; } //use this property to store the selected floor.
    public List<GenericType> FloorList { get; set; }
}
public class GenericType
{
    public string Name { get; set; }
    public string Id { get; set; }
    public GenericType(string id, string name)
    {
        Id = id;
        Name = name;
    } 
}

Controller: Controller:

    public IActionResult Index6()
    {
        //set initial data.
        FloorBuilder builder = new FloorBuilder()
        {
            SubmittedFloors = new List<string>(),
            FloorList = new List<GenericType>()
            {
                new GenericType("1001", "One"),
                new GenericType("1002", "Two"),
                new GenericType("1003", "Three"),
                new GenericType("1004", "Four"),
                new GenericType("1005", "Five"),
            }
        };

        return View(builder);
    }
    [HttpPost]
    public IActionResult Index6(FloorBuilder floorBuilder)
    {
        //do something
        return View();
    }

View Page:查看页面:

@model WebApplication6.Models.FloorBuilder;
<div class="row">
    <div class="col-md-4">
        <form asp-action="Index6">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div> 
            <div class="form-group">
                @Html.DropDownListFor(m => m.SubmittedFloors, new SelectList(Model.FloorList, "Id", "Name"),
                new { @multiple = "multiple", @class = "form-control multiselect", @id = "ddlselect" })
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
             
        </form>
    </div>
</div>
@section Scripts{
    <style>
        .caret {
            display: none !important;
        }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css">
    <script type="text/javascript">
        $(function () {  
            $("#ddlselect").multiselect({
                includeSelectAllOption: true,
                nonSelectedText: "-- Please Select --", 
            });
        });
    </script> 
}

The result as below:结果如下:

在此处输入图像描述

Updated:更新:

To set the default selected value, you could use a hidden field to store the selected value.要设置默认选定值,您可以使用隐藏字段来存储选定值。 Then, when page load, use JQuery to get the selected value, and then, based on it to add selected attribute for the DropDownlist options.然后,在页面加载时,使用 JQuery 获取选中值,然后在此基础上为 DropDownlist 选项添加选中属性。 Code like this:像这样的代码:

        <div class="form-group">
            @{ 
                var defaultselected = string.Join(",", Model.SubmittedFloors);
            }
            <input type="hidden" value="@defaultselected" id="hid_defaultselected" />
            @Html.DropDownListFor(m => m.SubmittedFloors, new SelectList(Model.FloorList, "Id", "Name"),
            new { @multiple = "multiple", @class = "form-control multiselect", @id = "ddlselect" })
            <input type="submit" value="Create" class="btn btn-primary" />
        </div>

JavaScript scripts: JavaScript 脚本:

<script type="text/javascript">
    $(function () {  
        //use a hidden field to store the default selected value. 
        var defaultvalue = $("#hid_defaultselected").val().split(",");
        //according to the default selected value to add the selected attribute for the DropDownList options.
        $("#ddlselect option").each(function (index, item) {
            if (jQuery.inArray($(item).val(), defaultvalue) != -1) {
                $(item).attr('selected', 'selected');
            }
        });
        //use bootstrap multiselect.
        $("#ddlselect").multiselect({
            includeSelectAllOption: true,
            nonSelectedText: "-- Please Select --", 
        });
    });
</script> 

Controller: Controller:

    public IActionResult Index6()
    {
        FloorBuilder builder = new FloorBuilder()
        {
            SubmittedFloors = new List<string>() {"1001","1002", "1005" }, //set the default selected value.
            FloorList = new List<GenericType>()
            {
                new GenericType("1001", "One"),
                new GenericType("1002", "Two"),
                new GenericType("1003", "Three"),
                new GenericType("1004", "Four"),
                new GenericType("1005", "Five"),
            }
        };

        return View(builder);
    }

The result like this:结果是这样的:

在此处输入图像描述

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

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