LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 5315 min read

Models

Learn how to design model and view-model classes to pass data between controllers and views.

What is a Model?

In MVC, a model is simply a plain C# class describing data — often matching a database table, but not always.

public class Task
{
public int Id { get; set; }
public string Title { get; set; } = "";
public bool IsComplete { get; set; }
}

View Models

A view model is a class shaped specifically for what a view needs to display — often combining data from multiple sources, or excluding fields the view shouldn't see.

public class TaskListViewModel
{
public List<Task> Tasks { get; set; } = new();
public int CompletedCount { get; set; }
public string PageTitle { get; set; } = "My Tasks";
}

Strongly Typed Views

Declaring `@model` at the top of a view gives you full IntelliSense and compile-time checking against that model's properties.

@model TaskListViewModel
<h1>@Model.PageTitle</h1>
<p>@Model.CompletedCount of @Model.Tasks.Count completed</p>

FAQs

View models let you shape exactly what the view needs — avoiding accidentally exposing sensitive fields, and decoupling your view from your database schema.

Summary

  • Models are plain classes describing data.
  • View models shape data specifically for what a view needs to render.
  • @model at the top of a view enables strongly typed, IntelliSense-backed access to it.
Next Lesson →

Model Binding & Validation