简体   繁体   English

ASP.NET MVC MultiSelectList 选中项不起作用

[英]ASP.NET MVC MultiSelectList selected item does not work

I trying to use the MultiSelectList in ASP.NET MVC with selected items.我尝试将 ASP.NET MVC 中的MultiSelectList与选定项目一起使用。

This is the backend code:这是后端代码:

var listPoles = new List<long>();

foreach (var pole in employeePole.PolesList)
                listPoles.Add(pole.Id);

            ViewBag.poles = new MultiSelectList(GetPoles(), "Id", "Name", listPoles);

GetPoles() returns IReadOnlyList . GetPoles()返回IReadOnlyList

HTML code (view): HTML 代码(查看):

<select multiple="multiple" title="selecione..." class="form-control selectpicker" id="poles" asp-for="@Model.Poles" asp-items="@ViewBag.poles"></select>

In edit view (front-end) the select component does not load the selected items but I used the exactly same code (with different entity) in another CRUD in the same system and it works fine.在编辑视图(前端)中,select 组件不会加载所选项目,但我在同一系统的另一个 CRUD 中使用了完全相同的代码(具有不同的实体),它工作正常。

I tried to use a lot of different types of collection (enumerable, readonlylist, list, etc) to create the selected items list but nothing works我尝试使用许多不同类型的集合(可枚举、只读列表、列表等)来创建所选项目列表,但没有任何效果

What can possibly be wrong?什么可能是错的?

  1. Try to debug if the list is filled with data.如果列表中充满了数据,请尝试调试。
  2. Don't invoke GetPoles() method into MultiSelectList constructor, use a variable for this for clean code reasons不要在 MultiSelectList 构造函数中调用 GetPoles() 方法,出于简洁的代码原因,为此使用变量
  3. Check if GetPoles() does not contain asynchronous method检查 GetPoles() 是否不包含异步方法
  4. Viewbag can be cleaned after read. Viewbag 可以在阅读后进行清洁。 Try to use the model instead.尝试改用 model。

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

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