← Back to ASP.NET Core Tutorial Home

Lesson 9: Models and Data Binding

Models represent the data used by your application. They are one of the core parts of MVC. In ASP.NET Core, models often describe real-world objects such as students, products, books, or customers.

Example of a simple model

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Course { get; set; }
    public int Age { get; set; }
}

This model represents a student object. Each property holds one piece of student information.

Sending a model to a view

A controller can create a model object and pass it to a view:

public IActionResult Details()
{
    Student student = new Student
    {
        Id = 1,
        Name = "Alice",
        Course = "Computer Science",
        Age = 21
    };

    return View(student);
}

Using a model in a view

The view must declare the model type first:

@model Student

@Model.Name

Course: @Model.Course

Age: @Model.Age

What is data binding?

Data binding is the process by which ASP.NET Core automatically maps incoming form data to model properties. This makes form handling much easier.

For example, if a form contains input fields named Name, Course, and Age, ASP.NET Core can bind those values directly to a Student object.

[HttpPost]
public IActionResult Create(Student student)
{
    return View(student);
}
Power of data binding: Instead of reading every form field manually, ASP.NET Core can build the model for you.

Benefits of models

Conclusion

Models are essential because they give your application a structured way to work with data. Data binding makes the interaction between forms and models simple and efficient.

← Previous Lesson Next Lesson: Passing Data with ViewBag, ViewData, and TempData →