Reputation: 327
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
Upvotes: 6
Views: 18544
Reputation: 1
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>
Upvotes: 0
Reputation: 83
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
Upvotes: 0
Reputation: 29
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
Upvotes: 0
Reputation: 61
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" />
Upvotes: 6
Reputation: 1874
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")) />
Upvotes: 8
Reputation: 10927
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.
Upvotes: 5
Reputation: 18290
It is an issue with:
asp-for="SendReport"
. That is not valid because the tag is input of type checkbox soasp-for
is expecting aboolean
and you are sending it a something else than a boolean.
Refer this:
Asp.net MVC putting checkbox data in a list of booleans
Upvotes: 2