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

Noxious Reptile
  • 838
  • 1
  • 7
  • 24
  • you haven't set a value on your check box. see my answer here http://stackoverflow.com/questions/22174475/multiple-radio-button-groups-in-mvc-4-razor/22174654#22174654 – Matt Bodily Dec 17 '15 at 14:03
  • What do you mean its not showing? You have a `CheckBoxFor()` method that will generate the html (a `` and a ``) 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. –  Dec 17 '15 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 – Noxious Reptile Dec 18 '15 at 10:04

3 Answers3

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>
vincentsty
  • 2,963
  • 7
  • 34
  • 51
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" } })
Saim Abdullah
  • 174
  • 14
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" })

davaus
  • 1,145
  • 13
  • 16