Creating and Using Partial Views in ASP.NET MVC

Scott Walker · Oct 11, 2011

To create a reusable UI component in ASP.NET MVC, you can add a partial view to your project. When adding the view, check the option for Create a partial view (.ascx). If your partial view uses a model, also select Create a strongly-typed view and choose the appropriate View data class.

Partial View Declaration

Below is an example of a strongly-typed partial view that accepts a collection of Blog entities.


<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<MVC_Test.Domain.Entities.Blog>>" %>

<% foreach (var blog in Model) { %>
    <!-- Render blog content here -->
<% } %>

Rendering Model Properties Inside the Partial View

Your partial view can output model properties just like a normal view:


<%: Model.Property2 %>

<h3><%: Model.Property3 %></h3>

Partial views are typically placed inside the Views/Shared folder so they can be reused across multiple views.


Using the Partial View

You can render the partial view from another view using either RenderPartial or Partial.

RenderPartial (writes directly to the response stream)


<% Html.RenderPartial("PartialViewName", ProductModel); %>

Partial (returns an MvcHtmlString)


<% Html.Partial("PartialViewName"); %>

RenderPartial is ideal for large amounts of data because it writes output directly to the response stream. Partial returns a string, so it is less efficient for large datasets.

This pattern makes it easy to break your UI into reusable components while keeping your views clean and maintainable.

Partial Views

Comments (0)

Please sign in to comment.