asp-for in checkbox throws and error in asp.net core

Viewed 10049

Here i have used two checkbox

<label asp-for="SendReport" class="checkbox-inline"><input type="checkbox" asp-for="SendReport" />Send Report</label><br>
<label asp-for="SendInvoice" class="checkbox-inline"><input type="checkbox" asp-for="SendInvoice" />Send Invoice</label><br>

and data type for both checboc i.e asp-for="SendReport" and asp-for="SendInvoice" is bit.
Now the problem is that when ever i try to load the page its throws error as below

Unexpected 'asp-for' expression result type 'System.Nullable`1[[System.Boolean, System.Private.CoreLib, Version=4.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'.<br>


Any help will be heartly thankful. Thank you

6 Answers

This is because asp-for is always expecting a boolean parameter when input type is checkbox, or a string parameter that can be parsed as boolean.

This said, having the assumption that SendReport and SendInvoice are not boolean:

Unexpected 'asp-for' expression result type 'System.Nullable`

you will have to change these parameters into boolean parameters that will be false by default, or into string parameters that will contain "false" or "true" by default.

I ran into the same issue when working with EFcore. From the database, I have:

namespace Project.EFCoreModels;
public partial class Account 
{
    public int AccountId {get; set;}
    public bool? HasSaleTax {get; set;}    
}

I want to display HasSalesTax as a checkbox input with asp tag helper:

<input type="checkbox" asp-for="HasSaleTax" class="form-control" />

This thows the error because the the input type checkbox does not accept nullable value. Fair enough.

To work around it, I created another partial class, same namespace, but on a different project directory (folder). This is a common pattern in .NetCore MVC when we want to add MetaData to an EFCore class.

namespace Project.EFCoreModels;
public partial class Account 
{         
    public bool SalesTaxInput 
    {
        get => this.HasSaleTax.GetValueOrDefault(); 
        set {this.HasSaleTax = value;} 
    }    
}

Then on the view, you just bind the input with the newly created prop:

 <input type="checkbox" asp-for="SalesTaxInput" class="form-control" />

When happened use bit datatype for reparent true, false but column allow null checked

You can fixed by two way

First: Set Not Allow null or remove ? in filed

Example:

public bool? SendReport {get; set; } 
// change to below code 
public bool SendReport {get; set; } 

Second: You can use normal html tag for checkbox input

<input type="checkbox" id="SendReport" name="SendReport" @((Model.SendReport== false ? "" : "checked")) />

If you use a

@Html.EditorFor(m => m.SendReport, new { @class = "form-control" }}

it will give you a dropdown box with options

<select id="SendReport" name="SendReport" class="list-box tri-state"> 
    <option value="">Not Set</option>
    <option value="true">True</option>
    <option value="false">False</option>
</select>

at least I do, when using Bootstrap 4 and Visual Studio 2019, with .NET Core 3.1

Anyone running into this MVC core abandon the "for" @Html.CheckBox("tblSubCategory.Enabled",Model.tblSubCategory.Enabled.HasValue? Model.tblSubCategory.Enabled:false)

tblSubCategory is a sub model on a view, Enabled is a nullable boolean

Related