Skip to main content

This is a new service.

Error summary if needed

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

Model ErrorSummaryIfNeededExampleDefaultViewModel.cs

using GovUkDesignSystemDotNet;

public class ErrorSummaryIfNeededExampleDefaultViewModel
{
    [GovUkValidateRequired(ErrorMessageIfMissing = "Please enter your first name")]
    public string FirstName { get; set; }
    
    [GovUkValidateRequired(ErrorMessageIfMissing = "Please enter your last name")]
    public string LastName { get; set; }
}

View ErrorSummaryIfNeededExampleDefault.cshtml

@using GovUkDesignSystemDotNet
@model ErrorSummaryIfNeededExampleDefaultViewModel

<form method="post">
    @await Html.GovUkErrorSummaryIfNeeded([
        nameof(Model.FirstName),
        nameof(Model.LastName),
    ])

    @await Html.GovUkFieldset(new FieldsetViewModel
    {
        Legend = new FieldsetLegendViewModel
        {
            HtmlOrText = new HtmlOrText("Enter your name"),
            IsPageHeading = true,
            Classes = ["govuk-fieldset__legend--l"]
        },
        HtmlOrText = new HtmlOrText(
            @<text>
                @await Html.GovUkTextInputFor(
                    m => m.FirstName,
                    new TextInputViewModel
                    {
                        Label = new LabelViewModel
                        {
                            HtmlOrText = new HtmlOrText("First name")
                        }
                    })

                @await Html.GovUkTextInputFor(
                    m => m.LastName,
                    new TextInputViewModel
                    {
                        Label = new LabelViewModel
                        {
                            HtmlOrText = new HtmlOrText("Last name")
                        }
                    })
             </text>)
    })

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

Controller

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

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