ASP.NET Core MVC Mastery
Lesson 3 of 58 5% of course

Layouts & Partial Views in Razor

30 · 8 min · 5/23/2026

Sign in to track progress and bookmarks.

Mastering Layouts, Partial Views & Rendering (Professional UI Architecture)

Building a maintainable UI is about more than just HTML. It's about avoiding duplication (DRY) and ensuring your 50+ pages look consistent and load fast.

1. The _Layout.cshtml (The Master Template)

The layout is your global shell. It handles the <head>, navigation, and footers. Any page-specific content is injected into the @RenderBody() method.

🔥 Expert Tip: Optional Design Sections

Sometimes a page needs a specific CSS file or a Google Maps script that other pages don't. Instead of loading it globally (slowing down the whole site), use Sections.


// In your _Layout.cshtml
<head>
    <link rel="stylesheet" href="~/css/site.css" />
    @RenderSection("Styles", required: false)
</head>

// In your ProductDetails.cshtml
@section Styles {
    <link rel="stylesheet" href="~/css/gallery-zoom.css" />
}
                

2. Partial Views: Modular UI Components

A Partial View is a reusable .cshtml file that contains a small snippet of HTML. It is perfect for headers, navigation, or repetitive "Product Cards" in a list.

Example: Reusable Customer Card


// _CustomerCard.cshtml
@model Toolliyo.Models.Customer

<div class="card p-3 shadow-sm">
    <h4>@Model.Name</h4>
    <p>Joined on @Model.JoinDate.ToShortDateString()</p>
    <button class="btn btn-primary">View Profile</button>
</div>

// Usage in Index.cshtml
@foreach(var customer in Model.TopCustomers) {
    <partial name="_CustomerCard" model="customer" />
}
                

3. Strongly Typed Views & ViewModels

In a 12-year expert architect's workflow, we never pass database entities directly to the view. We use a **ViewModel**. It only contains what the UI needs, preventing Over-Posting Attacks and keeping your view type-safe.


// The ViewModel
public class UserProfileViewModel {
    public string DisplayName { get; set; }
    public string PhotoUrl { get; set; }
    public int UnreadCount { get; set; }
}

// The UI (Razor)
@model UserProfileViewModel
<div class="profile-header text-white">
    <img src="@Model.PhotoUrl" alt="User Avatar" />
    <h1>Welcome, @Model.DisplayName</h1>
    <span class="badge">@Model.UnreadCount Messages</span>
</div>
                

Advanced: ViewComponents (Total UI Decoupling)

Partial Views are light, but if a component needs its own logic (e.g. fetching the latest news from a database), a Partial View is too weak. You would have to pass the news data from every single controller. ViewComponents solve this by having their own C# class and database logic independent of the controller.

Architect Insight: ViewComponent Performance

Senior architects use Async ViewComponents for any non-trivial logic. Because they run asynchronously, the browser can start rendering the rest of your layout while the component fetches data. This is how you build high-performance dashboards with 20+ dynamic widgets without making the page feel "heavy" to the user!

Test your knowledge

Quizzes linked to this course—pass to earn certificates.

Browse all quizzes
ASP.NET Core MVC Mastery

On this page

1. The _Layout.cshtml (The Master Template) 🔥 Expert Tip: Optional Design Sections 2. Partial Views: Modular UI Components 3. Strongly Typed Views & ViewModels Advanced: ViewComponents (Total UI Decoupling)
1. Core Framework
Introduction to ASP.NET Core MVC
MODULE 1: INTRODUCTION & ENVIRONMENT SETUP
Microsoft Web Stack Overview Evolution of ASP.NET Environment Setup
2. View Engine
Layouts & Partial Views in Razor
MODULE 2: .NET CORE FUNDAMENTALS
Core Concepts Project Structure Startup Flow Middleware Pipeline
MODULE 3: ASP.NET CORE BASICS
Creating Project CLI Commands wwwroot & Static Files
MODULE 4: MVC FUNDAMENTALS
MVC Architecture Dependency Injection (DI) Service Lifetimes
MODULE 5: DATA PASSING TECHNIQUES
ViewData vs ViewBag TempData ViewModel Pattern
MODULE 6: ROUTING
Conventional vs Attribute Routing Custom Constraints
MODULE 7: VIEWS & UI
Razor View Engine Layouts & Sections View Components
MODULE 8: ACTION RESULTS
ViewResult JsonResult RedirectResult
MODULE 9: HTML HELPERS
Form Helpers Custom HTML Helpers
MODULE 10: TAG HELPERS
Built-in Tag Helpers Custom Tag Helpers
MODULE 11: MODEL BINDING
FromQuery vs FromRoute Complex Binding
MODULE 12: VALIDATION
Data Annotations Remote Validation Fluent Validation
MODULE 13: STATE MANAGEMENT
Cookies & Sessions TempData
MODULE 14: FILTERS & SECURITY
Action Filters Authorize Filters Anti-forgery
MODULE 15: ENTITY FRAMEWORK CORE (DEEP DIVE)
DbContext Migrations LINQ Relationships
MODULE 16: DESIGN PATTERNS
Repository Pattern Unit of Work Clean Architecture
MODULE 17: FILE HANDLING
File Upload/Download PDF/Excel Generation
MODULE 18: ADVANCED ASP.NET CORE
Request Lifecycle Bundling & Minification Deployment
MODULE 19: PERFORMANCE & BEST PRACTICES
Caching Strategies Async Programming Secure Coding
MODULE 20: RAZOR PAGES (BONUS)
Razor Pages vs MVC
MODULE 21: REAL-WORLD PROJECTS (🔥 MUST DO)
E-Commerce Web Application Employee Management System