Answered by:
Checkbox issues in Razor Pages.

Question
-
User-190697402 posted
Hi All,
I need one urgent help for this. I have to insert checkbox value to sql database which i could acheive.
When i go to the edit page,its throwing the following error
InvalidOperationException: Unexpected expression result value 'Others' for asp-for. 'Others' cannot be parsed as a 'System.Boolean'.
My Model Class is as shown below.
public class RequestForm{ [Key] public int ID { get; set; } public string OtherCompetitorsChecked { get; set; } public string OtherCompetitorName { get; set; } }
And My RequestFrom.cshtml is
<div class="tr"> <div class="td"> <input id="ChkOthers" style="margin-left:40px;" asp-for="RequestForm.OtherCompetitorsChecked" type="checkbox" value="Others" /> Others </div> <div class="td"> <input id="CompetitorsOthersName" title="Please Fill In Other Competitor Name" asp-for="RequestForm.OtherCompetitorName" type="text" class="form-control-slp" required disabled style="width:50%" /> </div> </div>
im able to insert the data successfully,when i try to retieve the data in edit its throwing error,because its not boolean ,how can i solve this.Any help would be appreciated.
Friday, May 7, 2021 8:51 AM
Answers
-
User-821857111 posted
<input id="ChkOthers" style="margin-left:40px;" name="RequestForm.OtherCompetitorsChecked" type="checkbox" value="@Model.RequestForm.OtherCompetitorsChecked" checked="@(ModelRequestForm.OtherCompetitorsChecked == "Others") /> Others
Note that the checked attribute can accept a Razor expression that evaluates to true, false or null. If it evaluates to false or null, the checked attribute is not rendered at all.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, May 9, 2021 6:32 AM
All replies
-
User-821857111 posted
You can't bind strings to the asp-for attribute of a checkbox helper. You can only bind boolean values: https://www.learnrazorpages.com/razor-pages/forms/checkboxes#razor-checkboxes
Change the properties of your RequestForm class to bool instead of string.
Friday, May 7, 2021 12:04 PM -
User-190697402 posted
Hi Mikesdotnetting,
I want to insert the value of checkbox as a string not as true or false. I managed to solve the error by removing asp-for taghelper.
Now what is the issue is the selected checkbox is not checked in edit view , i tried to give condition like this
<input id="ChkOthers" style="margin-left:40px;" name="RequestForm.OtherCompetitorsChecked" type="checkbox" value="RequestForm.OtherCompetitorsChecked" @(RequestForm.OtherCompetitorsChecked == "Others"?"checked":"") /> Others
The name 'RequestForm' does not exist in the current context. When i use RequestForm for name and value attribute its working properly.
Could you please provide me a solution
Saturday, May 8, 2021 5:17 PM -
User-474980206 posted
The rendered value of the checkbox is not “Other”. View source and see. View source will then tell you much about your html generation. The value should be what you want the postback value to be if checked. You probably wanted to be “Other” rather than the name of the field. The value of the also makes no sense,
Saturday, May 8, 2021 10:15 PM -
User-821857111 posted
<input id="ChkOthers" style="margin-left:40px;" name="RequestForm.OtherCompetitorsChecked" type="checkbox" value="@Model.RequestForm.OtherCompetitorsChecked" checked="@(ModelRequestForm.OtherCompetitorsChecked == "Others") /> Others
Note that the checked attribute can accept a Razor expression that evaluates to true, false or null. If it evaluates to false or null, the checked attribute is not rendered at all.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, May 9, 2021 6:32 AM -
User-190697402 posted
Thank you so much Mikesdotnetting . I managed to fix the issue.
Sunday, May 9, 2021 7:44 AM