Validating model

Built-in server-side model validation using ASP.NET Core data annotations

Model binding and validation are available when you create a ASP.NET Core web app. Both automatically occur before the execution of a page handler. So, the OnPostAsync page handler only needs to verify the outcome of that validation.

The complete code to the PizzaModel class is NOT shown in this article.

if (!ModelState.IsValid)
{
    return Page();
}
  • In the preceding code, ModelState represents errors from model binding and validation.

  • If the ModelState is invalid, then the Pizza page is presented again to the user.

  • If the ModelState is valid, the OnPostAsync page handler calls upon an instance of PizzaService.

  • PizzaService is responsible for storing the information - in this case, using an in-memory data store.

Model binding and validation automatically occur before the execution of a page handler!

Define validation rules for the Pizza model using data annotations

Your new PizzaModel class gained access to any model types defined in the RazorPagesPizza.Models namespace, including the Pizza model, with the following using directive:

using RazorPagesPizza.Models;

Examine the Pizza model class:

using System.ComponentModel.DataAnnotations;

namespace RazorPagesPizza.Models
{
    public class Pizza
    {
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }
        public PizzaSize Size { get; set; }
        public bool IsGlutenFree { get; set; }

        [Range(0.01, 9999.99)]
        public decimal Price { get; set; }
    }

    public enum PizzaSize { Small, Medium, Large }
}

Data annotations are attributes that specify behaviors you want to enforce on the model properties to which they're applied.

The Pizza class uses the:

  • [Required] attribute to indicate that a property must have a value.

  • [Range] attribute to constrain a value to a specific range.

If you decide to enforce more validation rules, you can easily modify attributes in just one place, the Pizza model, without being required to modify any of the PageModel class files in the project. A significant benefit!

Note: A comprehensive set of data annotation attributes is available to you in the System.ComponentModel.DataAnnotations namespace. For the scope of this module, a simplified example is provided.

The Pizza model as a data transfer object

The Pizza model also serves as a Data Transfer Object (DTO). A DTO is an object that defines the data that will be sent over the network, in this case to the web API. In a more advanced version of this application, the RazorPagesPizza project's PizzaService class would use the Pizza model as a DTO that defines valid Pizza data that can be sent to and received from the web API or backing database.

A DTO is an object that defines the data that will be sent over the network, in this case to the web API

Next, you'll update the PizzaModel to interact with the PizzaService class to list existing pizzas and create new ones.

Last updated