Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Html.CheckboxFor not displaying in mvc

Tags:

asp.net-mvc

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

like image 652
Noxious Reptile Avatar asked Sep 25 '22 22:09

Noxious Reptile


2 Answers

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>
like image 157
vincentsty Avatar answered Oct 10 '22 20:10

vincentsty


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" } })
like image 44
Saim Abdullah Avatar answered Oct 10 '22 21:10

Saim Abdullah