5

The checkbox is not displaying in page. I tried many solutions in google. Nothing worked. Here s the code:

@model project.gamestatus

@using (Html.BeginForm("Create", "Calculator", FormMethod.Post, new { id = "frmID" }))
{

        //other codes

        <div class="form-group">
                @Html.Label("Show on Screen", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.CheckBoxFor(m => m.display_status)
                    @Html.ValidationMessageFor(model => model.display_status, "", new { @class = "text-danger" })
                </div>
            </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-info" />
            </div>
        </div>
}

Only if the checkbox is shown in page i can proceed with validation. In my view there is no checkbox

3
  • you haven't set a value on your check box. see my answer here stackoverflow.com/questions/22174475/… Commented Dec 17, 2015 at 14:03
  • What do you mean its not showing? You have a CheckBoxFor() method that will generate the html (a <input type="checkbox" .. /> and a <input type="hidden" ... />) for the property which you can easily verify by inspectinh the page source. If you not seeing it visually, then you probably have an issue with your css. Commented Dec 17, 2015 at 23:42
  • @Stephen Muecke When i inspected the html code i found it has a css property "display:none" along with it. so the checkbox is not showing in view Commented Dec 18, 2015 at 10:04

3 Answers 3

2

First of all, you need to ensure that the display_status model is of boolean type and assigned the display name to it along with any validation.

[Display(Name="CheckBox Display Name")]
[Required]
public bool display_status { get; set; }

Also, @Html.CheckBoxFor do not support the label of checkbox. Therefore, you can have the label of the checkbox using @Html.LabelFor as follow:

<div class="form-group">
    @Html.Label("Show on Screen", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.CheckBoxFor(m => m.display_status)
        @Html.LabelFor(m => m.display_status)
        @Html.ValidationMessageFor(m => m.display_status, "", new { @class = "text-danger" })
    </div>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

i checked in inspector and it always has a css property "display:none" which is autogenerated by the helper
2

though its a very old post but I undergo this issue todat, but when I inspected I found that the CSS property opacity was set to 0 so changing it to 1 will solve the issue

 @Html.EditorFor(model => model.Active, new { htmlAttributes = new { style = "opacity: 1" } })

Comments

0

I fixed the problem by setting the height to 20px explicitly. When I examined the page, it was being set to 20%:

@Html.CheckBoxFor(m => m.ExistingUser, new { style="height:20px" })

Comments

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.