asp-for in checkbox throws and error in asp.net core
Asked Answered
A

7

6

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

Aden answered 31/10, 2018 at 7:10 Comment(1)
Is there any reason you bind a bit field to checkbox? checkbox is binded by true or false, how did you decide when to check the checkbox by bit? What is the value for bit field?Trauner
C
8

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")) />
Charcoal answered 1/3, 2021 at 7:34 Comment(0)
P
6

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" />
Plug answered 4/3, 2021 at 22:12 Comment(0)
F
5

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.

Foreword answered 31/10, 2018 at 7:18 Comment(0)
S
2

It is an issue with: asp-for="SendReport". That is not valid because the tag is input of type checkbox so asp-for is expecting a boolean and you are sending it a something else than a boolean.

Refer this:
Asp.net MVC putting checkbox data in a list of booleans

Stringent answered 31/10, 2018 at 7:19 Comment(1)
Specifically, a nullable boolean (bool?) is being sentSkellum
P
0

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

Postfree answered 22/8, 2021 at 17:42 Comment(0)
C
0

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

Cosmopolite answered 22/9, 2022 at 7:34 Comment(0)
P
0

Just removing ? do not work. for me worked as i wrapped around a div claaa=checkbox

        <div class="form-group">
            <label asp-for="IsDeleted" class="control-label"></label>
            <div class="col-md-10">
                <div class="checkbox">
                    <input asp-for="IsDeleted" class="form-check" />
                    <span asp-validation-for="IsDeleted" class="text-danger"></span>
                </div>
            </div>
        </div>
Plafker answered 14/11, 2022 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.