user2284341
user2284341

Reputation: 671

Html.CheckBox not rendering

I have an html helper checkbox on a view in an MVC3 project:

<%
            var temp = Model.NonResident;
            if (Model.NonResident)
               Html.CheckBox("IsNonResident", true);
           else
               Html.CheckBox("IsNonResident", false);
        %>

The model field 'NonResident does have a value of true. I assigned the value to 'temp' and stepped through it. When I debug, the codee does hit the Html.CheckBox("IsNonResident", true) segment but it doesn't render.

I've checked 'View Source' and the control is not there. If I remove the 'if' statement, it does render if I use:

<%=Html.CheckBox("IsNonResident", true)%>

It must be something simple but I can't see it. Can anyone help?

Upvotes: 1

Views: 466

Answers (1)

recursive
recursive

Reputation: 86084

The difference is the equals sign in <%=Html.CheckBox("IsNonResident", true)%>. That outputs the result. With your if block, you are ignoring the result, so the output never makes it to the http response. One solution is to inline it like this:

<%=Html.CheckBox("IsNonResident", Model.NonResident)%>

Upvotes: 1

Related Questions