locked
What is difference between Partial View and Layout View? RRS feed

  • Question

  • User221424154 posted

    When we Use Partial view in our mvc Application and when we use Layout view?

    Tuesday, January 16, 2018 6:40 PM

Answers

  • User1400794712 posted

    Hi Manishamani,

    Layout view is often used to set the layout of the page. For example, if each page of a web application need the same layout, we can set the same part in layout view and use @RenderBody() to reserve space for body part.

    Partial view is used to reduce the duplication of view content(not include the layout part).

    In a view, it is divided like this:

    Main view:

    @{
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    <h2>PartialIndex</h2>
    @{Html.RenderAction("TestPartial", new { id = 1 });}
    @{Html.RenderAction("TestPartial", new { id = 2 });}
    @{Html.RenderAction("TestPartial", new { id = 3 });}
    @{Html.RenderAction("TestPartial", new { id = 4 });}
    @{Html.RenderAction("TestPartial", new { id = 5 });}

    Best Regards,

    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 17, 2018 9:42 AM

All replies

  • User475983607 posted

    When we Use Partial view in our mvc Application and when we use Layout view?

    Please see the openly published reference documentaiton.

    A layout view provides a consistent layout for a site.  

    https://docs.microsoft.com/en-us/aspnet/core/mvc/views/layout

    A partial view is a reusable component used within a View.

    https://docs.microsoft.com/en-us/aspnet/core/mvc/views/partial

    Tuesday, January 16, 2018 6:47 PM
  • User1400794712 posted

    Hi Manishamani,

    Layout view is often used to set the layout of the page. For example, if each page of a web application need the same layout, we can set the same part in layout view and use @RenderBody() to reserve space for body part.

    Partial view is used to reduce the duplication of view content(not include the layout part).

    In a view, it is divided like this:

    Main view:

    @{
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    <h2>PartialIndex</h2>
    @{Html.RenderAction("TestPartial", new { id = 1 });}
    @{Html.RenderAction("TestPartial", new { id = 2 });}
    @{Html.RenderAction("TestPartial", new { id = 3 });}
    @{Html.RenderAction("TestPartial", new { id = 4 });}
    @{Html.RenderAction("TestPartial", new { id = 5 });}

    Best Regards,

    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 17, 2018 9:42 AM