locked
bool not saving in Db RRS feed

  • Question

  • User1571524970 posted

    Hi guys,

    I have a view which includes a form taking in various information (10+ input fields of strings, ints and a bool). When I submit this form and the controller saves it into the Database, all data except the bool is saved. I can not see why this is happening, it is only bool Surveys that does not save and the rest of the data saves into the Database as normal. Has this something to do with bool being a nullable value? Or has it something to do with entity framework saving it as a 'bit' into the Db? I can not understand this at all

    My view (Surveys is at the end):

    <form asp-controller="School" asp-action="BookingForm" method="post">
    
        <br />
    
        <p>Please complete the following information</p>
        
       
        <div>
            <label for="Date">Choose Camp Date:</label>
            <br />
            <input asp-for="Booking.Date" id="txtDate" name="Date" value="@Model.Date.Value.ToShortDateString()" />
        </div>
        <br />
        <div>
            <label for="OfficialSchoolName">School Name:</label>
            <br />
            <input asp-for="Booking.OfficialSchoolName" id="txtOfficialSchoolName" name="OfficialSchoolName" value="@Model.OfficialSchoolName" />
        </div>
        <br />
        <div>
            <label for="Address">School Address:</label>
            <br />
            <input asp-for="Booking.Address1" id="txtAddress1" name="Address1" value="@Model.Address1" />
            <br />
            <input asp-for="Booking.Address2" id="txtAddress2" name="Address2" value="@Model.Address2" />
            <br />
            <input asp-for="Booking.Address3" id="txtAddress3" name="Address3" value="@Model.Address3" />
            <br />
            <input asp-for="Booking.Address4" id="txtAddress4" name="Address4" value="@Model.Address4" />
        </div>
        <br />
        <div>
            <label for="Times">Times Needed:</label>
            <br />
            <input asp-for="Booking.StartTime" id="txtStartTime" name="StartTime" value="Start Time e.g. 10:30" />
            <input asp-for="Booking.EndTime" id="txtEndTime" name="EndTime" value="End Time e.g. 16:00" />
        </div>
        <br />
        <div>
            <label for="ClassGroups">Class Groups:</label>
            <br />
            <input asp-for="Booking.ClassGroups" id="txtClassGroups" name="ClassGroups" value="e.g. 4th class, 6th class, 1st year etc." />
        </div>
        <br />
        <div>
            <label for="Topics">Topics to be covered:</label>
            <br />
            <input asp-for="Booking.Topics" id="txtTopics" name="Topics" value="" />
        </div>
        <br />
        <div>
            <label for="TeacherName">Teacher Name:</label>
            <br />
            <input asp-for="Booking.TeacherName" id="txtTeacherName" name="TeacherName" />
            @Html.Raw(HttpUtility.HtmlDecode(Html.ValidationMessageFor(model => model.TeacherName).ToHtmlString()))
    
        </div>
        <br />
        <div>
            <label for="Email">Teacher Email:</label>
            <br />
            <input asp-for="Booking.Email" id="txtEmail" name="Email" />
        </div>
        <br />
        <div>
            <label for="Surveys">Would you like to take part in research surveys?</label>
            <br />
            <input asp-for="Booking.Surveys" id="Surveys" name="Surveys" type="checkbox" checked />
        </div>
        <br />
        <input type="submit" value="Submit" class="submit" />
    </form>
    

    Model which includes bool? Surveys

    public string OfficialSchoolName { get; set; }
            public string TeacherName { get; set; }
            public string Address1 { get; set; }
            public string Address2 { get; set; }
            public string Address3 { get; set; }
            public string Address4 { get; set; }
            public string Eircode { get; set; }
            public string County { get; set; }
            public string Email { get; set; }
            public string PhoneNumber { get; set; }
            public string StartTime { get; set; }
            public string EndTime { get; set; }
            public bool? Surveys { get; set; } ***

    Can anyone tell me why Surveys will not save into the Db and all the other data saves without any problems?

    Cheers :)

    Wednesday, August 21, 2019 12:49 AM

Answers

  • User1520731567 posted

    Hi darego,

            <input asp-for="Booking.Surveys" id="Surveys" name="Surveys" type="checkbox" checked />
    

    Because above html doesn't have value attribute.

    I suggest you could modify ,like:

    <input asp-for="Surveys" id="Surveys" name="Surveys" type="checkbox" checked onclick="$(this).val(this.checked ? true : false)"/>
    

    But you need note:

    Look at the definition of the parameter bool?

    The '?' means the bool type is nullable. It can be True | False | Null.

    Because it now has three values, You can't simply check for true | false. You must check for null as well.

    And some browsers don't ignore NULL, and some ignore NULL.

    Different browsers will have different performances.

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 21, 2019 9:22 AM
  • User1520731567 posted

    Hi darego,

    darego

    Although how do differentiate between NULL and False? i.e. if i leave the checkbox unchecked and submit the form, it saves NULL into the Db instead of False.

    The checkbox is only checked and unchecked.

    If unchecked,how do you know it is NULL or FALSE?

    Unless you then add hints by judging NULL and FALSE in the front end.

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 22, 2019 2:24 AM

All replies

  • User1120430333 posted
    You can define that DB table column as bit or int to hold 0 = true and 1 = false.

    https://www.cs.uic.edu/~jbell/CourseNotes/C_Programming/Decisions.html

    You set the bool value in the C# or whatever code language you are using to true or false based on the data you saved to the database representing the bool value of 0 or 1.
    Wednesday, August 21, 2019 2:35 AM
  • User1520731567 posted

    Hi darego,

            <input asp-for="Booking.Surveys" id="Surveys" name="Surveys" type="checkbox" checked />
    

    Because above html doesn't have value attribute.

    I suggest you could modify ,like:

    <input asp-for="Surveys" id="Surveys" name="Surveys" type="checkbox" checked onclick="$(this).val(this.checked ? true : false)"/>
    

    But you need note:

    Look at the definition of the parameter bool?

    The '?' means the bool type is nullable. It can be True | False | Null.

    Because it now has three values, You can't simply check for true | false. You must check for null as well.

    And some browsers don't ignore NULL, and some ignore NULL.

    Different browsers will have different performances.

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 21, 2019 9:22 AM
  • User1571524970 posted

    thanks for the 2 replies guys.

    i changed my input as you suggested and now when i click true, it saves into the Database. Although how do differentiate between NULL and False? i.e. if i leave the checkbox unchecked and submit the form, it saves NULL into the Db instead of False. So in Booking Details view, if Surveys is False, it shows as 'Not Set' instead of false

    <input asp-for="Booking.Surveys" id="Surveys" name="Surveys" type="checkbox" onclick="$(this).val(this.checked ? true : false)" />

    Thanks for the help

    Wednesday, August 21, 2019 11:12 AM
  • User1520731567 posted

    Hi darego,

    darego

    Although how do differentiate between NULL and False? i.e. if i leave the checkbox unchecked and submit the form, it saves NULL into the Db instead of False.

    The checkbox is only checked and unchecked.

    If unchecked,how do you know it is NULL or FALSE?

    Unless you then add hints by judging NULL and FALSE in the front end.

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 22, 2019 2:24 AM
  • User1120430333 posted
    If it's null, then assume it is false.
    Thursday, August 22, 2019 2:54 AM
  • User1571524970 posted

    Thanks for the help lads

    Thursday, August 22, 2019 1:38 PM