I want to do a multiple selector with checkboxes in ASP.NET MVC it fetches the employee ID from a Sql database

<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>

””””’My Controller Looks Like this””””” My controller handles the brings back the information i have on the datebase of which its all the names of the employees i have created

string 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 });

Answer

Try using

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

Leave a Reply

Your email address will not be published. Required fields are marked *