ASP.NET Core MVC Beginner

Views in ASP.NET Core MVC

Views are Razor files that turn data from a controller into HTML. This lesson covers where views live, how MVC finds the right one, the difference between strongly typed models and ViewData or ViewBag, and how to keep views simple.

How a view becomes HTML
  1. StartAction returns View(model)For example in ProfileController.Index
  2. Step 2View is locatedViews/Profile/Index.cshtml, then Views/Shared/Index.cshtml
  3. Step 3_ViewStart runsSets the layout for the view
  4. Step 4View rendersRazor writes HTML using the model
  5. Step 5Layout wraps itThe view output is placed where the layout calls RenderBody
  6. EndHTML is sentThe browser receives one complete page

The idea in plain words

A view is the presentation part of MVC. It is a .cshtml file written in Razor, a syntax that lets you mix HTML with small bits of C#. The controller prepares the data, and the view decides how that data looks on the page. A good view reads almost like plain HTML with a few placeholders.

Views live in the Views folder. Each controller gets a subfolder named after it, so views for HomeController sit in Views/Home. Views used by many controllers, such as layouts and shared partial views, go in Views/Shared.

  • Views/Home/Index.cshtml: the Index view of HomeController.
  • Views/Shared/_Layout.cshtml: the common page frame used by most views.
  • Views/_ViewImports.cshtml: using directives and Tag Helpers shared by all views.
  • Views/_ViewStart.cshtml: code that runs before each view, usually to set the layout.

How MVC finds a view

When an action calls return View(), MVC looks for a view with the same name as the action. It first checks Views/[ControllerName]/[ActionName].cshtml and then Views/Shared/[ActionName].cshtml. You can pass a different name, as in View("Summary"), or a full path starting with a tilde when you need to.

Relying on the convention keeps controllers short. Passing explicit names is useful when two actions share one view, for example Create and Edit using the same form.

In Visual Studio 2026 you rarely create the file by hand: right-click inside the action method and choose Add View. Razor View - Empty gives a blank view; Razor View lets you pick a template (List, Details, Create, Edit, Delete) and a model class, and Visual Studio writes a working page for you. Either way the file lands in Views/[ControllerName]/[ActionName].cshtml, so MVC finds it by convention.

Example: a strongly typed view

The safest way to pass data is a model class. The action passes an instance to View, and the view declares the type with @model. You then get IntelliSense and compile-time checks, and a typo in a property name becomes a build error instead of an empty spot on the page.

C#
public class ProfileViewModel
{
    public string Name { get; set; } = "";
    public List<string> Skills { get; set; } = new();
}

public class ProfileController : Controller
{
    public IActionResult Index()
    {
        var model = new ProfileViewModel
        {
            Name = "Asha",
            Skills = new() { "C#", "ASP.NET Core", "SQL" }
        };
        ViewData["Title"] = "My profile";
        return View(model);
    }
}

The matching view

The view reads the model through the Model property. ViewData["Title"] is read by the layout to fill the page title. Razor HTML-encodes values written with @, so user input is displayed safely.

Razor
@* Views/Profile/Index.cshtml *@
@model ProfileViewModel

<h1>@Model.Name</h1>

@if (Model.Skills.Count == 0)
{
    <p>No skills added yet.</p>
}
else
{
    <ul>
        @foreach (var skill in Model.Skills)
        {
            <li>@skill</li>
        }
    </ul>
}

ViewData, ViewBag and when to avoid them

ViewData is a dictionary keyed by strings, and ViewBag is a dynamic wrapper over the same data. They are handy for small extras such as the page title, but they have no compile-time checking and you have to cast values when reading them. For the main data of a page, use a view model class.

A view model is a class shaped for one screen. It can combine data from several sources and leave out fields the page should not see, which is safer than passing your database entities straight to the view.

Common mistakes

Watch for these when your views start to grow.

  • Putting queries or business rules in a view. Prepare everything in the controller or a service.
  • Using ViewBag for everything, which hides typos until run time.
  • Passing EF Core entities directly to views, which can expose fields and trigger lazy loading.
  • Using Html.Raw on user input, which turns off encoding and opens the door to cross-site scripting.

Going further

Layouts give every page a shared header and footer, partial views reuse a piece of markup, and view components combine reusable markup with their own logic. Tag Helpers make forms and links read like normal HTML. Each of these has its own lesson later in the course.

Interview question

What is the difference between ViewData, ViewBag and a strongly typed model?

ViewData is a string-keyed dictionary and ViewBag is a dynamic wrapper over the same storage, so both lack compile-time checking and values may need casting. A strongly typed model is passed to View(model) and declared with @model, giving IntelliSense and build-time errors for mistakes. Use a view model for the main page data and keep ViewData or ViewBag for small extras.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.