I am developping an asp.net mvc application.
I have a viewmodel :
public class UserViewModel
{
public bool IsActive {get;set;}
}
And I have the associated razor view :
<span>@Html.CheckBoxFor(d => d.IsActive)</span>
The problem with this is that I have only the checkbox in my view without any label, I would like to know if it's possible to indicate a label in the CheckBoxFor parameter or by adding an attribute on my property in the ViewModel.
You can use Data Annotations. Add this at the top of your ViewModel class definition
Add
[Display(Name = "Active")]above theIsActivepropertyand add
@Html.LabelFor(d => d.IsActive)in your viewwhich will render
<label for="IsActive">Active</label>before the checkbox.