Models in ASP.NET Core MVC

Every web application works with data. From displaying product details to handling user input, data flows continuously between the server and the user interface.

To manage this data effectively, an application needs a clear structure that defines what data looks like and how it should be handled.

In ASP.NET Core MVC, this structure is defined using Models.

Models represent the data of your application and play a central role in how information moves between controllers and views.


What is a Model in MVC?

A model is a class that represents the structure of data in your application.

It defines what properties exist and what type of values they hold.

For example, in an e-commerce application, a product has a name, price, and identifier. A model is used to represent this structure inside the application.

Think of a model as a blueprint of data. It ensures that data is organized and consistent throughout the application.


Creating Your First Model

Let’s create a simple model for a product.

File: Models/Product.cs

namespace MyMvcApp.Models
{
    public class Product
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public decimal Price { get; set; }
    }
}

This model defines what a product looks like inside your application.


Why Models Are Important

Without models, data handling becomes unstructured and difficult to manage.

Models provide:

  • A clear structure for data
  • Consistency across the application
  • A way to pass data between controller and view

They ensure that every part of the application understands data in the same way.


How Models Fit in MVC Flow

Let’s connect this with the MVC flow you have already learned.

  1. User sends a request
  2. Controller processes the request
  3. Controller prepares data using a Model
  4. Model is passed to the View
  5. View displays the data

This is how data moves inside an MVC application.


Using Model in Controller

Controllers use models to prepare and send data to views.

using MyMvcApp.Models;
using Microsoft.AspNetCore.Mvc;

namespace MyMvcApp.Controllers
{
    public class ProductController : Controller
    {
        public IActionResult Details()
        {
            var product = new Product
            {
                Id = 1,
                Name = "Laptop",
                Price = 75000
            };

            return View(product);
        }
    }
}

What happens here?

  • A model object is created
  • Data is assigned to its properties
  • The model is passed to the view

Displaying Model Data in Razor View

The view receives this model and displays it to the user.

File: Views/Product/Details.cshtml

<div>

    <h2>Product Details</h2>

    <p>
        <strong>Id:</strong>
        @Model.Id
    </p>

    <p>
        <strong>Name:</strong>
        @Model.Name
    </p>

    <p>
        <strong>Price:</strong>
        @Model.Price
    </p>

</div>

This is how data flows from the model to the UI.


A Practical Understanding

Consider a real scenario — a user opens a product page.

The application needs to:

  • Fetch product data
  • Store it in a structured format
  • Send it to the view
  • Display it on the screen

The model acts as that structured format, making the entire process clean and manageable.


Models and Data Input (Form Handling Preview)

So far, we have used models to send data from the controller to the view. But models are also used in the opposite direction — to receive data from the user.

In real applications, users interact with forms such as registration, login, or product creation. The data entered in these forms is sent to the controller.

Instead of handling each value manually, ASP.NET Core MVC automatically maps form data to a model.


Example: Form Using Model

File: Views/Product/Create.cshtml

<form 
    method="post" 
    asp-action="Create">

    <input 
        name="Name" 
        placeholder="Enter Product Name" />

    <button>
        Save
    </button>

</form>

Controller Action Receiving Model

[HttpPost]
public IActionResult Create(Product model)
{
    // Model automatically gets values from form

    // Example: model.Name, model.Price

    return RedirectToAction("Details");
}

What is Happening Here?

  • User fills the form and submits it
  • Form data is sent to the controller
  • ASP.NET Core automatically maps values to the model
  • Controller receives a fully populated object

This automatic mapping is called Model Binding, which we will explore in detail in upcoming Lessons.


Important Understanding

At this point, you might think that the same model can be used everywhere in the application.

However, in real-world scenarios, the data required by the view is often different from the actual model.

This leads us to an important concept — ViewModels.


Understanding Data Flow in MVC

Now we know how models play a central role in how data moves between different parts of the application. To understand this clearly, it is important to look at the complete flow of data during a request.

When a user sends a request, it first reaches the controller. The controller is responsible for preparing the data required by the view. This data is typically stored inside a model object.

For example, a controller may create a product object and pass it to the view:

return View(product);

The view receives this model and displays its values using Razor syntax such as @Model.Name and @Model.Price. At this stage, the model is used to send data from the controller to the view.

The flow changes when the user interacts with the application. Consider a form where the user enters product details. When the form is submitted, the entered values are sent back to the controller.

Instead of manually reading each value, ASP.NET Core automatically maps the incoming data to a model:

public IActionResult Create(Product model)

If the form contains fields like Name and Price, these values are assigned to the corresponding properties of the model object.

This demonstrates that models are used in both directions — first to provide data to the view, and then to receive user input back into the application.

This two-way data flow forms the foundation of how MVC applications handle and process data.


Summary

Models define the structure of data in ASP.NET Core MVC. They help organize data and enable communication between controllers and views.

Understanding models is the first step toward understanding how data flows through an MVC application.

In the next section, we will explore ViewModels, which are one of the most important concepts in MVC and are used in almost every real-world application.