Thursday, 11 July 2013

Checkboxes Beside Labels in ASP.Net MVC4

This had me stumped for a while, so here’s what you need to do:

Inside your form,

@Html.CheckBoxFor(m => m.XX)
@Html.LabelFor(m => m.XX, new {@class="checkbox"})

This is for a boolean XX in the model, marked with the appropriate attributes to get the label text. The important thing is the class attribute for the label, because that applies the style defined in the default css:

label.checkbox { display: inline;}

To make it appear on the same line as the checkbox itself.