Skip to content
Learn Netverks

Lesson

Step 19/36 53% through track

models-viewmodels

Models and view models

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Models and view models: the syntax, APIs, and habits you need before advancing in ASP.NET.

Teams ship Models and view models on every ASP.NET codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Models and view models 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.

Models represent domain or database entities. View models shape data specifically for a view or API response—avoid exposing internal fields or navigation properties directly to clients.

Why separate view models

  • Views need computed fields (FullName, formatted dates)
  • Forms need fields not on the entity (ConfirmPassword)
  • APIs need stable contracts decoupled from EF entities
public class RegisterViewModel {
    public string Email { get; set; } = "";
    public string Password { get; set; } = "";
    public string ConfirmPassword { get; set; } = "";
}

Mapping

Map entity ↔ view model in the controller or a dedicated mapper (manual, AutoMapper, or Mapster)—keep EF entities out of Razor when possible.

Important interview questions and answers

  1. Q: Entity vs DTO vs view model?
    A: Entity maps to DB; DTO crosses API boundaries; view model fits a specific UI screen.
  2. Q: Over-posting attack?
    A: Binding directly to entities lets attackers set privileged fields—use view models with whitelisted properties.

Self-check

  1. Why not pass EF entities to every Razor view?
  2. What field belongs on RegisterViewModel but not User entity?

Interview prep

What is over-posting?

Binding directly to entities lets attackers set privileged fields (e.g. IsAdmin)—use view models with whitelisted properties for input.

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

  • DTO vs entity?
  • Validation on VM?

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