简体   繁体   English

.Net MVC 5 多选列表框

[英].Net MVC 5 Multi-Select List Box

I'm trying to create a multi-select box and so far tried many things.我正在尝试创建一个多选框,到目前为止尝试了很多东西。

public class DepartmentDropDown
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class DeviceUserReportViewModel
{
    public List<int> DepartmentIds { get; set; } 
    public List<DepartmentDropDown> Departments { get; set; } 
}
public IActionResult DeviceUserReport()
{
    IEnumerable<DepartmentDropDown> departments = _unitOfWork.Repository<Department>().Get().Select(s=> new DepartmentDropDown { Id = s.Id, Name = s.Name });
    
    DeviceUserReportViewModel model = new DeviceUserReportViewModel
    {
        Departments = departments
    };
}

And in the DeviceUserReport.cshtml并在 DeviceUserReport.cshtml

@Html.ListBoxFor(s => s.DepartmentIds,new MultiSelectList(Model.Departments,"Id","Name"),new { @class = "form-control"})

It still shows the list box like this.它仍然像这样显示列表框。

在此处输入图像描述

For all developers asking the same question =>对于所有提出相同问题的开发人员 =>

In a.Net MVC application, without any addition library, most you can do is this:在 .Net MVC 应用程序中,没有任何附加库,您最多可以做的是:

在此处输入图像描述

With this jQuery library, you can turn a.Net MVC Multi-Select Box to this:使用这个jQuery 库,你可以把一个 .Net MVC Multi-Select Box 变成这样:

在此处输入图像描述

Add this js and css to your application将此 js 和 css 添加到您的应用程序

And mark your input area as multiselect with $('#mySelectList').multiSelect();并使用$('#mySelectList').multiSelect();将您的输入区域标记为多选

Note: When used the codes from question, this multi-select list sends a list int in model to server side.注意:当使用问题中的代码时,此多选列表将 model 中的列表 int 发送到服务器端。

Html.ListBoxFor Extension Method creates ListBox control and allows users to select multiple options in one go. It is very useful when you want to get multiple options for the users. Html.ListBoxFor Extension Method 创建ListBox控件并允许用户将多个选项合二为一 select go。当您想为用户获取多个选项时,它非常有用。

You can define this control as follows: @Html.ListBoxFor(model => model.property, ItemList, object HtmlAttribute) Model => model.property: It contains the ID of selected items ItemList: It is a list of items Object HtmlAttributes: It sets extra attributes to ListBox Control.您可以按如下方式定义此控件: @Html.ListBoxFor(model => model.property, ItemList, object HtmlAttribute) Model => model.property:它包含所选项目的 ID ItemList:它是一个项目列表HtmlAttributes:它为 ListBox 控件设置额外的属性。

Like a multiple-selection list box, a standard list box allows users to select values in a list.与多选列表框一样,标准列表框允许用户在列表中选择 select 个值。 However, with a list box, users can select only one item in the list.但是,使用列表框,用户只能 select 列表中的一项。 Like a multiple-selection list box, a list box displays all of the items in the list by default.与多选列表框一样,列表框默认显示列表中的所有项目。

Here your code is perfect.在这里你的代码是完美的。 so to select multiple item from the list just press Ctrl + mouse click.因此,对于列表中的 select 多个项目,只需按Ctrl + 鼠标单击即可。

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

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