Click here to Skip to main content
Click here to Skip to main content

Partial View in ASP.NET MVC 4

, 8 Jul 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This article will help you to create partial views in mvc 4 with razor.


If you want to reuse a view in your web application, you can go for the partial view concept. Partial view is like a regular view with a file extension .cshtml. We can use partial views in a situation where we need a header, footer reused for an MVC web application. We can say that it’s like a user control concept in ASP.NET.

Using the code

Here I am going to explain how to create a partial view in an MVC 4 ASP.NET application.

First add a view to the shared folder with the view name _Product. The best way to create a partial view is with the name preceded by '_', because the name specifying that it is reusable.

Here in this example, I am using the partial view to display the item selected in the webgrid. Creating the webgrid example is in the below link:

Add HTML controls in the partial view to display the selected item:

@model PartialViewSample.Models.Product

    Layout = null;

<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />
            @Html.TextBox("Id", Model.Id)
            @Html.TextBox("Name", Model.Id)
            @Html.TextBox("Description", Model.Description)
            @Html.TextBox("Quantity", Model.Quantity)

We can call the partial view in a normal view like:

Html.RenderPartial("~/Views/Shared/_Product.cshtml", product);


@Html.Partial("~/Views/Shared/_Product.cshtml", product);

Html.Partial returns a string, Html.RenderPartial calls Write internally, and returns void. You can store the output of Html.Partial in a variable, or return it from a function. You cannot do this with Html.RenderPartial because the result will be written to the Response stream during execution. So @html.RenderPartial() has faster execution than @html.Partial() due to RenderPartial giving quick response to the output.

We can call the partial view if the grid has a selected item. The code block is shown here:

@if (grid.HasSelection)
    product =(PartialViewSample.Models.Product)grid.Rows[grid.SelectedIndex].Value;        
    Html.RenderPartial("~/Views/Shared/_Product.cshtml", product);           


  • 4th July, 2013: Initial post. 

This tip is based on this blog.


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


About the Author


India India
No Biography provided

Comments and Discussions

GeneralMy vote of 4 Pinmemberamol_th10hrs 29mins ago 
GeneralGreat work PinmemberLuai Kayyali26-Jan-15 6:32 
SuggestionWorking with Partial View in MVC Razor PinmemberMayank Tripathi11-Dec-14 21:04 
GeneralMy Vote of 5 PinprofessionalBhavik_Patel2-Sep-14 1:02 
GeneralMy vote of 1 PinprofessionalYogesh Kumar Tyagi23-Jul-14 0:20 
SuggestionThe Best Example for Partial View PinmemberAShivaPrasad2-Jul-14 4:28 
GeneralMy vote of 2 PinmemberAShivaPrasad2-Jul-14 4:07 
QuestionWhere's the rest of the code PinmemberBill Warner16-May-14 7:29 
QuestionNice article Pinmemberpuja1119127-Feb-14 23:26 
QuestionWhat about REAL reuse? PinmemberJVMFX14-Feb-14 9:44 
Question4.5 stars PinmemberJanilane18-Dec-13 17:27 
GeneralMy vote of 3 PinmemberAnthony Fassett26-Nov-13 4:59 
QuestionGood Job 5 PinmemberStephen Wu 719-Nov-13 12:25 
QuestionGood Job! PinmemberMember 1036586328-Oct-13 10:20 
GeneralMy vote of 5 PinprofessionalAmir Mohammad Nasrollahi27-Jul-13 23:11 
QuestionAdd the source code in downloadable format PinmemberTridip Bhattacharjee8-Jul-13 6:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150128.1 | Last Updated 8 Jul 2013
Article Copyright 2013 by josh-jw
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid