Click here to Skip to main content
15,880,725 members
Articles / Web Development / ASP.NET / ASP.NET Core

ASP.NET Core 2.0 MVC Partial Views

Rate me:
Please Sign up or sign in to vote.
1.80/5 (2 votes)
28 Aug 2017CPOL1 min read 20.7K   2  
How to reuse parts of web pages using partial views in ASP.NET Core MVC. Continue reading...

Problem

How to reuse parts of web pages using partial views in ASP.NET Core MVC.

Solution

In an empty project, update Startup class to add services and middleware for MVC:

C#
public void ConfigureServices(
            IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env)
        {
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

Add a model to display in the view:

C#
public class EmployeeViewModel
    {
        public int Id { get; set; }
        public string Firstname { get; set; }
        public string Surname { get; set; }
        public AddressViewModel Address { get; set; }
    }

    public class AddressViewModel
    {
        public string Line1 { get; set; }
        public string Line2 { get; set; }
        public string Line3 { get; set; }
    }

Add a controller with action method returning ViewResult:

C#
public IActionResult Index()
        {
            var model = new EmployeeViewModel
            {
                Id = 1,
                Firstname = "James",
                Surname = "Bond",
                Address = new AddressViewModel
                {
                    Line1 = "Secret Location",
                    Line2 = "London",
                    Line3 = "UK"
                }
            };
            return View(model);
        }

Add parent view Index.cshtml:

HTML
@using Fiver.Mvc.PartialViews.Models.Home
@model EmployeeViewModel

<div style="border: 1px solid black; margin: 5px">
    <h2>Employee Details (parent view)</h2>

    <p>Firstname: @Model.Firstname</p>
    <p>Surname: @Model.Surname</p>

    @Html.Partial("_Address.cshtml", Model.Address)
</div>

Add partial view _Address.cshtml:

C#
@using Fiver.Mvc.PartialViews.Models.Home
@model AddressViewModel

<div style="border: 1px dashed red; margin: 5px">
    <h3>Address Details (partial view)</h3>

    <p>Lin1: @Model.Line1</p>
    <p>Line2: @Model.Line2</p>
    <p>Line3: @Model.Line3</p>
</div>

Discussion

Partial views are special type of views that are rendered inside other views. They are useful for reusing parts of a view or splitting a large view into smaller components.

Partial views are created like regular views and can be returned via the controller action method using ViewResult. The key difference is that they don’t run _ViewStart.cshtml before their rendering and are usually rendered inside another view.

Partial views are rendered in views using @Html.Partial() method, passing in the name of partial view and optionally a model. Partial view name can be absolute or relative path and view engine will search the current folder or shared folder.

Partial View gets a copy of parent view’s ViewData dictionary. You could also pass a model into it, which is normally part of parent’s page view model.

Note: ASP.NET Core provides an alternate and more flexible approach to reusing or splitting views that can run code and render views without relying on parent views. Read more about View Components.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
-- There are no messages in this forum --