RJPWilliams
RJPWilliams

Reputation: 43

ASP.NET Core 6 MVC Razor : checkbox list showing checked when value of column is false

I wanted to set up a list with checkboxes on a reminder column to remind people to continue with their application which could be ticked and then returned to the controller to update the source applicants table.

After much searching, I found the answer, create a virtual table which takes the data from the source, send that to the page where those requiring reminders can be ticked, then, on return, put it back into the source. Works fine.

The following is the beginning of the CheckBoxItem model:

public class CheckBoxItem
{
    public int Id { get; set; }

    public DateTime DateRegistered { get; set; }
    public bool SendReminder1 { get; set; }
    public DateTime? Reminder1Sent { get; set; }
}

The applicants are then called from the applicants table:

Applicants = _Repository.GetApplicantsList_Registered_NotCompleted();

and the routine that creates the check box list then created

checkBoxItems = GetCheckBoxItems(Applicants, checkBoxItems);

The beginning of GetCheckBoxItems is:

private List<CheckBoxItem> GetCheckBoxItems(IList<Applicant> Applicants, List<CheckBoxItem> checkBoxItems)
{
    foreach (var applicant in Applicants)
        checkBoxItems.Add(new CheckBoxItem()
                              {
                                  Id = applicant.Id,
                                  DateRegistered = applicant.DateRegistered,
                                  SendReminder1 = blSendReminder1,
                                  Reminder1Sent = applicant.Reminder1Sent,
                              }
}

This is then displayed on the page with a CheckBoxFor and a HiddenFor:

@Html.CheckBoxFor(item => Model[i].SendReminder2)
@Html.HiddenFor(item => Model[i].Id)

The result shows checkboxes with the correct ticked/unticked depending on true/false values (next to the checkboxes is a temporary @Model[i].SendReminder1 to show the actual data). Checkbox with correct display

However I then put a button on the form that calls up a filtered version of the applicants data, filtering out those people that have already been reminded. When the filter is applied by the controller the checkboxes show a tick even though the boolean column SendReminder1 is actually storing false.

Attached is a screenshot of two checkbox showing true when the data is false, the pattern with the first checkbox unticked and the second and third ticked is the same as the full set of unfiltered data:

Checkbox showing true when data is false

I've examined the data being sent and it is false. I then thought that the CheckBoxItems must be remembering the original data even when overwritten, so I added a new list:

checkBoxItems = new List<CheckBoxItem>();

When that didn't solve the problem, I tried:

checkBoxItems.Clear();

Neither of these stops the checkbox from displaying the same pattern of true and false. Any ideas on how I get the CheckBoxItem list to be cleared completely?

Upvotes: 0

Views: 40

Answers (0)

Related Questions