1

First of all, thank you for your help. I'm pretty new.

This should be super simple but I've spent hours trying to figure this out. I have a form in my view. The form is populating everything correctly - except for the checkbox. I've researched and researched and tried a lot of different suggestions online. No matter what I try, the form sets the same value for checked and unchecked.

View:

<div class="checkbox">
    <label>
         <input id="lunch45" type="checkbox" value="true">45 minute lunch
    </label>
</div>

jQuery:

<script>
    $(document).ready(function () {
        $("#Lunchsubmit-form").click(function () {
            var model = {};
            model.EmployeeID = Number($("#lunchemployeeId").val());
            model.LunchTime = Date($('#lunchtimeId').val());
            model.PositionID = Number($("#lunchposition").val());
            model.LongerLunch = Boolean($('#lunch45').val());
            console.log("model", model);
            $.ajax({
                type: "HttpPost",
                url: "/Home/CreateLunch",
                dataType: 'json',
                data: JSON.stringify(model),
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.success)
                        window.location.href = "/Home/Index";
                    else
                        alert("Error: Lunch not submitted");
                },
                error: function () {
                    alert("Error: Lunch not submitted");
                }
            });
        });
    });
</script>

Model:

public class Lunch
{
    public int LunchID { get; set; }
    public int EmployeeID { get; set; }
    public DateTime LunchTime { get; set; }
    public int? PositionID { get; set; }
    public bool LongerLunch { get; set; }

    public virtual Employee Employee { get; set; }
    public virtual Position Position { get; set; }
}

Thanks again. I know how simple this should be but nothing's working.

4
  • <input id="lunch45" checked="checked" ... > or simple <input id="lunch45" checked ... > . Then the value can take any string or number you need. Commented Apr 7, 2020 at 5:03
  • Does this answer your question? Input Checkbox checked by default Commented Apr 7, 2020 at 5:08
  • Unfortunately it doesn't. I've tried that... I think there must be something else buggy in the code. All of those suggestions keep returning the same value. I updated it to this: <label><input type="checkbox" id="lunch45" checked value="true">45 minute lunch</label> and it returns true both ways..... I've tried to change true to false in the value - also tried to leave the value blank. Returns the same value both ways. Commented Apr 7, 2020 at 5:12
  • Try using 1 and 0 , false is not equal to 'false' Check this about Boolean function geeksforgeeks.org/javascript-boolean Commented Apr 7, 2020 at 11:47

1 Answer 1

1

Try using 1 and 0 , read about JS Boolean function here

Check

alert(Boolean(Number("0")));
alert(Boolean(Number("1")));

Then

  <input id="lunch45" type="checkbox" value="0" />
  ...
  model.LongerLunch = Boolean(Number($('#lunch45').val()));

Or use checkbox directly

model.LongerLunch = $('#lunch45').is(":checked");

Or maybe you need verify is checked, before get the value

<input id="lunch45" type="checkbox" value="0" >
<input id="lunchx" type="checkbox" value="1" checked>

alert($('#lunch45').is(":checked"));
alert($('#lunchx').is(":checked"));
Sign up to request clarification or add additional context in comments.

1 Comment

You are a genius! <input id="lunch45" type="checkbox" value="0" /> and model.LongerLunch = $('#lunch45').is(":checked"); worked great. I did have to change value="0" to value="" to make it save correctly to the database. But it worked great otherwise. Thank you!!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.