name property of the checkBox is throwing error the delegate type could not be inferred.

Anjali Agarwal 1,531 Reputation points
2023-02-13T23:54:23.33+00:00

I have the following Model class:

Public partial class EmployeeInfo

{

public int EmployeeInfoId { get; set; }

public string LastName { get; set; } = null!;

public string EmailAddress { get; set; } = null!;

public virtual Reassignment? Reassignment { get; set; }

}

public partial class Reassignment

{

public int ReassignmentId { get; set; }

public int EmployeeInfoId { get; set; }

public virtual EmployeeInfo EmployeeInfo { get; set; } = null!;

public virtual ICollection<ReassignmentSectionJoin> ReassignmentSectionJoins { get; } = new List<ReassignmentSectionJoin>();

}

public partial class ReassignmentSectionLookup

{

public int ReassignmentSectionLookupId { get; set; }

public string Category { get; set; } = null!;

public string Section { get; set; } = null!;

public virtual ICollection<ReassignmentSectionJoin> ReassignmentSectionJoins { get; } = new List<ReassignmentSectionJoin>();

}

public partial class ReassignmentSectionJoin

{

public int ReassignmentSectionJoinId { get; set; }

public int ReassignmentId { get; set; }

public int ReassignmentSectionLookupId { get; set; }

public virtual Reassignment Reassignment { get; set; } = null!;

public virtual ReassignmentSectionLookup ReassignmentSectionLookup { get; set; } = null!;

}

This is what I have in my controller class:

public async Task<IActionResult> Index()

{

       var ITSections= new List<ReassignmentSectionLookup>();

        ITSections= await _employeeService.GetITSections();

}

ViewData["ITSections"] = ITSections.Select(x => new SelectListItem

        {

            Value = x.ReassignmentSectionLookupId.ToString(),

            Text = x.Section

        }).ToList();

public async Task<List<ReassignmentSectionLookup>> GetITSections()

    {

        return await _ackContext.ReassignmentSectionLookups.Where(s => s.Category == "IT").ToListAsync(); ;

    }

My razor view looks like this:

<div>

                        <br /><b>ACR IT</b>  <br /><br />

                        @{

                            var ITSections = ViewData["ITSections"] as List<SelectListItem>;

                            if (ITSections != null && ITSections.Any())

                            {

                                foreach (var item in ITSections)

                                {

                                    <input type="checkbox" name"@Model.Reassignment.ReassignmentSectionJoins.ToList()[count].ReassignmentSectionLookup" value="@item.Value" @(Html.Raw(item.Selected ? "checked=\"checked\"" : "")) /> @item.Text  <br />

                                }

                            }

                        }

                    </div>

I am getting an error at this line

name="@Model.Reassignment.ReassignmentSectionJoins.ToList()[count].ReassignmentSectionLookup"

The error is:

The delegate type could not be inferred.

The issue is when I do HTTPPost. I dont see any value in the employeeInfo, but I do see value in selectedItems:

any help will be appreciated.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,803 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,598 questions
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 33,166 Reputation points Microsoft External Staff
    2023-02-14T06:38:43.4933333+00:00

    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:

    image1


    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

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.