Hi @Anjali Agarwal
So now the question is the NullReferenceException error, right?
For this issue, since this the create page, in the Get action method, you didn't return a EmployeeInfo model to the Create razor view, so, the page model (EmployeeInfo @Model
) is null, and the related navigation properties such as: Reassignment, ReassignmentSectionJoins, and ReassignmentSectionLookup also will be null. So, when using the @model
to bind the checkbox element it will show the NullReferenceException error.
To solve this error, in the Get action method, you can create a new EmployeeInfo entity and return it to the razor view, or you can bind the checkbox like this:
var ITSections = ViewData["ITSections"] as List<SelectListItem>;
if (ITSections != null && ITSections.Any())
{
foreach (var item in ITSections)
{
<input type="checkbox" name="Reassignment.ReassignmentSectionJoins[0].ReassignmentSectionLookup.SelectedSection"
value="@item.Value" @(Html.Raw(item.Selected ? "checked=\"checked\"" : "")) /> @item.Text <br />
}
}
In the ReassignmentSectionLookup class, add a SelectedSection property to store the checkbox checked value.
public partial class ReassignmentSectionLookup
{
public int ReassignmentSectionLookupId { get; set; }
public string Category { get; set; } = null!;
public string Section { get; set; } = null!;
//add this property to store the checkbox selected value
[NotMapped]
public string[] SelectedSection { get; set; }
public virtual ICollection<ReassignmentSectionJoin> ReassignmentSectionJoins { get; } = new List<ReassignmentSectionJoin>();
}
The result as below:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion