Skip to main content

This is a new service.

Checkbox item for

Read more about the checkboxes component on the Gov.UK Design System website

Model CheckboxItemForExampleDefaultViewModel.cs

using System.ComponentModel.DataAnnotations;

public class CheckboxItemForExampleDefaultViewModel
{
    [AllowedValues(true, ErrorMessage = "Confirm you agree to the declaration")]
    public bool ConfirmDeclaration { get; set; }
}

View CheckboxItemForExampleDefault.cshtml

@using GovUkDesignSystemDotNet
@model CheckboxItemForExampleDefaultViewModel

@await Html.GovUkErrorSummaryIfNeeded()

<form method="post">
    @await Html.GovUkCheckboxItemFor(
        m => m.ConfirmDeclaration,
        new CheckboxItemViewModel
        {
            Label = new LabelViewModel
            {
                HtmlOrText = new HtmlOrText("I declare that the information provided above is accurate")
            }
        }
    )

    @await Html.GovUkButton(new ButtonViewModel
    {
        HtmlOrText = new HtmlOrText("Validate")
    })
</form>

Controller

public IActionResult MyActionName(CheckboxItemForExampleDefaultViewModel viewModel)
{
    if (!ModelState.IsValid)
    {
        return View("CheckboxItemForExampleDefault", viewModel);
    }

    // Do some work with the validated view-model
    return View("InteractiveExampleValidationSuccessful");
}