Search code examples
c#htmlasp.net-mvc

When trying to tick checkboxes there's message 'asp-for' must be of type 'System.Boolean' or 'System.String' that can be parsed as a 'System.Boolean'


I know that there's a lot of topics about it on SO, but also on the web, but none of the answers found worked.

I'm working on an MVC application in ASP.Net and I'm need to create a table with a list of users and their accesses to some applications. So for each row, I have the user details and the last 4 columns are to check what accesses have been requested for them. To do so, I'm using checkboxes, via an input asp-for elements with type "checkbox" which looks like this:

<input asp-for="@item.StandardVDI" type="checkbox" checked="@item.StandardVDI" name="FiRMCHB" id="FiRMCHB">

My problem is that I have the error message, which seems to be a popular one:

"InvalidOperationException: Unexpected 'asp-for' expression result type'System.Nullable`1[[System.Boolean, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' for <input>. 'asp-for' must be of type 'System.Boolean' or 'System.String' that can be parsed as a 'System.Boolean' if 'type' is 'checkbox'.

I know that an input asp-for checkbox must have a boolean type, but that's the case with my model:

public bool? StandardVDI { get; set; }

I've tried to make it nullable or not, it's still the same

For testing purpose, I've forced the variable to have a default value (that was one of the solution I found on the web):

private bool? _standardVDI = false;
public bool? StandardVDI 
{
    get
    {
        return _standardVDI;
    }
    set { _standardVDI = false; } 
}

But, even though, the variable has a value as Null, I have the same error.

I was wondering if the problem was not linked to my LinQ query in controller, as the result shows null value:

var userList = (from u in _context.Users
                join w in _context.WV on u.ID equals w.mainXUI into wu
                from w in wu.DefaultIfEmpty()
                join g in _context.GS on u.ID equals g.mainXUI into gu
                from g in gu.DefaultIfEmpty()
                join f in _context.FRM on u.ID equals f.mainXUI 
                into fu
                from f in fu.DefaultIfEmpty()
                join i in _context.IGT on u.ID equals i.mainXUI into iu
                from i in iu.DefaultIfEmpty()

                select new Users()
                {
                    ID = u.ID,
                    Username = u.Username,
                    OfficeLocation = u.OfficeLocation,
                    ContractType = u.ContractType,
                    UserDept = u.UserDept,
                    MP = w.LSy,
                    Laptop = g.RIUserAccess,
                    StandardVDI =  f.userBitData,
                    DevDI = i.EC
                });

No matter what I'm trying, this LinQ query returns Null value.

Any idea of how I could have the checkboxes ticked according the _StandarVDI value?

Thanks for your help


Solution

  • You cannot pass null to checkboxes. Checkboxes can only be either true(checked) or false(unchecked). You need to handle what happens when the Linq query returns null. I would suggest you declared your attribute and property as non nullable like:

    private bool _standardVDI = false;
    
    public bool StandardVDI { get; set; }
    

    And then in your Linq query check if the f.userBitData data is null or not like the following:

    vm.StandardVDI = f.userBitData == null || f.userBitData == false ? false : true;
    

    You want to set the vm.StandardVDI to false if the Linq query returns either null or false

    Try it out and let me know how it goes.