Returning a Partial View from an Action

Move the part of the page that changes into a partial view, render it on the full page with the partial tag helper, and add an action that returns just that partial with PartialView, so a request gets only its HTML, without the layout.

Journeyman · ASP.NET Core · Razor · Updated

After this lesson you can

  • Return a partial view from an action with PartialView, so a request gets only part of a page

Key takeaways

  • Move the part that changes into a partial, and render it on the full page with <partial> inside a div with an id.
  • PartialView("_ProductGrid", model) renders only the partial: no layout, no navigation bar, no <html> or <head>.
  • One partial serves both the full page and the Grid action, so the two can never drift apart.
  • An action that returns a partial still checks its input, such as answering 404 for a page that doesn't exist.

The products page's grid of cards is about to be loaded two ways: as part of the whole page, and on its own. So it moves into a partial view of its own.

The partial

Views/Products/_ProductGrid.cshtml holds the grid. Its model is the page of products:

@model List<Product>

<div class="row row-cols-1 row-cols-sm-2 row-cols-lg-3 g-3">
    @foreach (Product product in Model)
    {
        <div class="col">
            <partial name="_ProductCard" model="product" />
        </div>
    }
</div>

Partials can use partials: each card is still _ProductCard, from Partial Views.

Views/Products/Index.cshtml now renders the grid with the partial tag helper, inside a div with an id, so JavaScript can find it and replace what's inside:

<div id="product-grid">
    <partial name="_ProductGrid" model="Model" />
</div>

The full page looks exactly as before.

The action

A new action returns only the partial:

// GET /products/grid?page=2: only the grid of product cards, for the page's JavaScript
[HttpGet("grid")]
public IActionResult Grid(int page = 1)
{
    int pageCount = (productDao.Count() + PageSize - 1) / PageSize;
    if (page < 1 || page > pageCount)
    {
        return NotFound();
    }

    return PartialView("_ProductGrid", productDao.GetPage(page, PageSize));
}

PartialView("_ProductGrid", model) renders the partial with that model, and nothing else: no _Layout.cshtml, no navigation bar, no <html> or <head>. Compare it with Index:

Index returns View(...) Grid returns PartialView(...)
Uses the layout Yes: the whole page No: just the partial
GET /products?page=2 / GET /products/grid?page=2 answers A full HTML page A <div class="row ..."> with 12 cards
Meant for The browser to show JavaScript to put into the page

It still checks the page number, and answers 404 for a page that doesn't exist, exactly like Index.

Trying it

Run the app and open /products/grid?page=2 in the browser. You'll see twelve unstyled cards: the HTML is all there, but without the layout there's no Bootstrap to style it. (Press Ctrl+U to see the HTML: it starts with the grid's <div>, and there's no <html> at all.)

That's exactly what the page's JavaScript needs: a piece to drop into a page that already has its styles.

Do it: check the page number before drawing the grid

Before Grid returns PartialView(...), it checks that the page asked for exists, and answers 404 when it doesn't. The practice box can't run an MVC app, so write that check as a plain C# method.

The method gets three values:

  • productCount: how many products there are, such as 77.
  • pageSize: how many cards fit on one page, such as 12.
  • page: the page number asked for, such as 7.

For example, 77 products at 12 to a page:

Page Products on it
1 to 6 12 each: 72 in all
7 the last 5
8 none: there is no page 8

So there are 7 pages. A part-full last page still counts as a page. PageExists(77, 12, 7) returns true, and PageExists(77, 12, 8) returns false.

Your job: write the method bool PageExists(int productCount, int pageSize, int page).

  • Work out how many pages there are: the products divided by the page size, counting a part-full last page as a page.
  • Return true when page is from 1 up to that number of pages.
  • Return false when page is 0 or less.
  • Return false when page is past the last page.

Practiced live on the lesson page: write the C# right there and check it.

Check yourself

1 question to try on the lesson page.

So what

One partial, two uses: the full page renders it with <partial>, and a small action returns it alone with PartialView. The cards are drawn by the same Razor in both cases, so they can never drift apart.

Key terms

Cheat sheets