Hello All,
Selecting the CheckBoxes on Page Load
I have the following two Model classes:
Public partial class EmployeeInfo
{
public int EmployeeInfoId { get; set; }
public virtual List<ReassignmentSection> ReassignmentSections { get; set; } = new List<ReassignmentSection>();
}
public class ReassignmentSection
{
public int ReassignmentSectionId { get; set; }
public int EmployeeInfoId { get; set; }
public int ReassignmentSectionLookupId { get; set; }
[NotMapped]
public string[] SelectedSection { get; set; }
public virtual EmployeeInfo EmployeeInfo { get; set; } = null!;
}
I have the following code on my controller:
ViewData["ITSections"] = ITSections.Select(x => new SelectListItem
{
Value = x.ReassignmentSectionLookupId.ToString(),
Text = x.Section
}).ToList();
On my razor page, I am binding the checkbox list like this:
@{
var ITSections = ViewData["ITSections"] as List<SelectListItem>;
if (ITSections != null && ITSections.Any())
{
foreach (var item in ITSections)
{
<input type="checkbox" name="Sections[0].SelectedSection" value="@item.Value" @(Html.Raw(item.Selected ? "checked=\"checked\"" : "")) /> @item.Text <br />
}
}
}
If the end user selects few CheckBoxes, I want those CheckBoxes to be pre selected.