11 November 2026

ASP.NET Core MVC for Students: Controllers, Models and Razor Views

  Asp.Net CS By Example       11 November 2026

Beginner tutorial

MVC separates an application into a Model (data), View (HTML) and Controller (request handling). This makes a larger web app easier to understand and test.

1. Create the project

dotnet new mvc -n StudentMvc
cd StudentMvc
dotnet run

Open the HTTPS or HTTP address printed by the command.

2. Add a model

namespace StudentMvc.Models;

public record Student(int Id, string Name, string Course);

Save this as Models/Student.cs. A record is a compact immutable data type.

3. Add a controller

using Microsoft.AspNetCore.Mvc;
using StudentMvc.Models;

namespace StudentMvc.Controllers;

public class StudentsController : Controller
{
    public IActionResult Index()
    {
        var students = new[]
        {
            new Student(1, "Asha", "C#"),
            new Student(2, "Ravi", "ASP.NET Core")
        };
        return View(students);
    }
}

Save as Controllers/StudentsController.cs. The action returns a view and passes the student array to it.

4. Add a Razor view

@model IEnumerable<StudentMvc.Models.Student>
@{ ViewData["Title"] = "Students"; }
<h1>Students</h1>
<table>
<tr><th>Name</th><th>Course</th></tr>
@foreach (var student in Model)
{
    <tr><td>@student.Name</td><td>@student.Course</td></tr>
}
</table>

Save as Views/Students/Index.cshtml. Browse to /Students.

Understand the flow

The URL selects StudentsController, Index selects the action, and MVC looks for Views/Students/Index.cshtml. Razor combines HTML with C# expressions. Keep validation and persistence in suitable services as the app grows.

Practice

Add an Email property and display it. Add a Details(int id) action and return NotFound when the ID does not exist.

Self-check

Controller actions coordinate work, models represent data, and views render the response. A view name and controller name form the default folder convention.

Return to the .NET Core learning path · Official MVC tutorial

logoblog

Thanks for reading ASP.NET Core MVC for Students: Controllers, Models and Razor Views

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.