我想在 ASP.NET MVC 中使用复选框做一个多重选择器,它从 Sql 数据库中获取员工 ID

Posted

技术标签:

【中文标题】我想在 ASP.NET MVC 中使用复选框做一个多重选择器,它从 Sql 数据库中获取员工 ID【英文标题】:I want to do a multiple selector with checkboxes in ASP.NET MVC it fetches the employee ID from a Sql database 【发布时间】:2021-06-01 15:10:39 【问题描述】:
<label asp-for="EmployeeId" class="control-label"></label>
                    @html.DropDownListFor(m => m.EmployeeId, (SelectList)ViewBag.LineManager, "Select",
                    new  @class = "form-control", multiple = "multiple")
                    @Html.ValidationMessageFor(model => model.EmployeeId, "", new  @class = "text-danger" )

                </div>

'''''''''我的控制器看起来像这样'''''''''' 我的控制器处理带回我在数据库中拥有的所有员工姓名的信息已创建

字符串 uri = "Vfl";

        var searchValue = HttpContext.Request.Form["search[value]"].FirstOrDefault();

        var start = Request.Form["start"].FirstOrDefault();

        // Paging Length 10,20  
        var length = Request.Form["length"].FirstOrDefault();


        int pageSize = length != null ? Convert.ToInt32(length) : 0;
        int skip = start != null ? Convert.ToInt32(start) : 0;
        int recordsTotal = 0;

        var response = _vflService.GetAll(uri, "");

        foreach(var item in response)
        
            item.EmployeeName = GetEmployeeName(item.ReponsiblePersonId);
        
        //Search
        if (!string.IsNullOrEmpty(searchValue))
        
            response = response.Where(m => m.EmployeeName.ToLower().Contains(searchValue.ToLower()) || (!string.IsNullOrEmpty(m.ImmediateCauseDescription) && m.ImmediateCauseDescription.ToLower().Contains(searchValue.ToLower())));
        

        recordsTotal = response.Count();

        //Paging   
        var data = response.Skip(skip).Take(pageSize).ToList();

        return Json(new  recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data );

【问题讨论】:

【参考方案1】:

您应该使用 MultiSelectList - https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.rendering.multiselectlist?view=aspnetcore-5.0

【讨论】:

【参考方案2】:

尝试使用

@Html.DropDownListFor(m => m.EmployeeId, (SelectList)ViewBag.LineManager, "Select", new  @class = "form-control",multiple = "multiple", size = "5" )

【讨论】:

以上是关于我想在 ASP.NET MVC 中使用复选框做一个多重选择器,它从 Sql 数据库中获取员工 ID的主要内容,如果未能解决你的问题,请参考以下文章

如何禁用 ASP.NET MVC 中 HtmlHelper 方法生成的单选按钮和复选框?

如何在 Asp.NET MVC 中使用复选框创建多选下拉菜单

如何在 ASP.NET MVC 中使用多个表单元素

使用 C# 在 ASP.NET MVC 中使用多个参数过滤数据

如何在我的 ASP.NET MVC 应用程序中使用 ACE?

为啥复选框选择列表总是在 ASP.NET MVC-5 中发布为 null [重复]