---
title: "Partial views"
slug: "partial-views"
abstract: "Understand when and how to build a partial view using the ASP.NET Core SDK."
---
# Partial views

[ASP.NET Core partial views](https://docs.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-8.0) render HTML output within another view's rendered output.

> [!note]
> The term *partial view* refers to one of the following:
>
> -   An ASP.NET Core Model-View-Controller (MVC) app, where markup files are called *views*.
>
> -   A Razor Pages app, where markup files are called *pages*.

The ASP.NET Core SDK uses MVC partial views for the lightweight components that make up a SitecoreAI page. For example, structural components that only contain placeholders.

Using the ASP.NET Core SDK, you build partial views by following the [standard MVC conventions](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller). Similarly, partial view discovery follows the [standard ASP.NET Core MVC rules](https://docs.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-3.1).

## Configuring a partial view

After you build a partial view following the standard MVC conventions, you must map the Experience Edge response to the partial view with the `AddPartialView<TModel>()` and the `AddDefaultPartialView()` extension methods.

### Example

Here's an example partial view. Note the following:

-  `AddPartialView("HeaderBlock", "_HeaderBlock")` maps the `HeaderBlock` response component to the  `_HeaderBlock` partial view.
- `AddPartialView(name => name.StartsWith("sc"), "_OtherBlock")` maps all response components prefixed with `sc` to the `_OtherBlock` partial view.
- `AddDefaultPartialView("_ComponentNotFound")` maps any response component unmatched by the previous two method calls to the `_ComponentNotFound` default partial view.

```csharp
public void ConfigureServices(IServiceCollection services)
{
  var renderingEngineBuilder = services.AddSitecoreRenderingEngine(options =>
    options

      // Map Partials
      .AddPartialView("HeaderBlock", "_HeaderBlock")
      .AddPartialView(name => name.StartsWith("sc"), "_OtherBlock")

      // Add fallback for any other component
      .AddDefaultPartialView("_ComponentNotFound");
  );
}

```

## Accessing SitecoreAI content from Razor views

To access the data that Experience Edge returns in a Razor view, you can add the following code to your Razor view:

```razor
@using MyRenderingHost.ViewModels;
@using Sitecore.AspNetCore.SDK.LayoutService.Client.Response.Model.Fields;

@{
  var route = this.SitecoreRoute();
  var context = this.SitecoreContext();
  var component = this.SitecoreComponent().ReadFields<ContentBlock>();
  var pageTitleField = route.ReadField<TextField>("pageTitle");
}

<section>
  <h1 asp-for="@pageTitleField"></h1>
  <h2>Site name: @context.Site.Name</h1>
  <h2>Route name: @route.Name</h1>
  <h2 asp-for="@component.Heading"></h2>
  <div>
    <sc-text asp-for="@component.Content"></sc-text>
  </div>
</section>

```

> [!note]
> The following Razor page extensions are available in the [`Sitecore.AspNetCore.SDK.RenderingEngine.Extensions`](https://sitecore.github.io/ASP.NET-Core-SDK/api/Sitecore.AspNetCore.SDK.RenderingEngine.Extensions.html) namespace, in the [`RazorPageExtensions`](https://sitecore.github.io/ASP.NET-Core-SDK/api/Sitecore.AspNetCore.SDK.RenderingEngine.Extensions.html) class:
>
> -   `SitecoreRoute` - get the current Sitecore `Route` object.
>
> -   `SitecoreContext` - get the current Sitecore `Context` object.
>
> -   `SitecoreComponent` - get the current Sitecore `Component` object.

You can read content from the Experience Edge response here, but you can't use model binding to read values. If you need to access route, context, or component values in your partial view, consider migrating from a partial view to a model-bound view.

## Migrating from partial view to model-bound view

Sometimes a partial view requires access to data from the Experience Edge response. This can be an indication that the partial view has become more complex than intended. In this case, we recommend that you migrate to a model-bound view.

To migrate from a partial view to a model-bound view:

1. Create a view model with the required Experience Edge response properties.
2. In the `Startup` class, replace the registration of the partial view with a model-bound view component registration.
3. Ensure that the Razor view is in the `/Views/Shared/Components/SitecoreComponent` folder.