Skip to content
Learn Netverks

Lesson

Step 23/36 64% through track

validation-data-annotations

Validation and data annotations

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Validation and data annotations: the syntax, APIs, and habits you need before advancing in ASP.NET.

Teams ship Validation and data annotations on every ASP.NET codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Validation and data annotations in contexts like: Line-of-business APIs, intranets, BFF layers, and cloud-hosted services on Linux or Windows.

Write C# (top-level or Program class), click Run on server—the dev runner uses dotnet build/run on a temp net8 project (requires .NET SDK; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

ASP.NET Core validates models using data annotations and ModelState. Invalid submissions re-render forms with error messages—same idea as Django forms or Java Bean Validation.

Common attributes

  • [Required], [StringLength(100)], [EmailAddress]
  • [Range(1, 100)], [Compare("Password")]
  • [RegularExpression] for custom patterns
public class LoginViewModel {
    [Required][EmailAddress]
    public string Email { get; set; } = "";
    [Required][DataType(DataType.Password)]
    public string Password { get; set; } = "";
}

Server-side check

if (!ModelState.IsValid)
    return View(model);

Client-side validation via jQuery Unobtrusive or similar is optional UX—never trust it alone.

Important interview questions and answers

  1. Q: FluentValidation vs annotations?
    A: Annotations are simple on properties; FluentValidation suits complex rules in separate classes.
  2. Q: API validation response?
    A: [ApiController] auto-returns 400 with ProblemDetails when ModelState invalid.

Self-check

  1. What checks ModelState in an MVC action?
  2. Why validate on the server even with client JS?

Tip: Re-display bound forms on validation failure so users do not retype everything—return View(model) with the same instance and show asp-validation-for messages.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • [Required] enough?
  • Custom validation attribute?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump