How to create partial views (MVC tutorial number 10)?

Questpond
Posted by in ASP.NET MVC category on for Intermediate level | Points: 250 | Views : 91649 red flag

This is the 10thMVC (Model view controller) tutorial and in this article we will try to understand how we can improve reusability using partial views.

This is the 10thMVC (Model view controller) tutorial and in this article we will try to understand how we can improve reusability using partial views. In case you are completely new to MVC (Model view controller), do have a look at the previous articles and videos given at the end of the article.

Alternatively you can also see these videos to come up to speed

My half-life has been spent on writing interview questions for Microsoft technology and I hope I keep writing them. You can see my video tutorial for the same by clicking on .NET,ASP.NET,C#,MVC Interview questions and answers

Introduction

When we talk about web application reusability is the key. So as a MVC developer we would like to create reusable views. For instance we would like to create reusable views like footer and header views and use them inside one big MVC view.

Reusable views can be achieved by creating “Partial views”.


Step 1:- Create a simple view

The first step would be to create a simple view with a controller. You can see from the below snapshot, I have created a simple view called as “Index.aspx” which will be invoked via “Homecontroller.cs”.


In case you are coming to this section directly please see the previous Labs to synch up.

Step 2:- Create a simple partial view

Now that we have created the main view it’s time to create a partial view which can be consumed inside the “Index” view. In order to create a partial view , right click on the view folder and mark the check box “Create a partial view” as shown in the below figure.


Step 3:- Put something in partial view

Put some text or logic in your partial view.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
This is reusable view

Step 4:- Call the partial view in the main

Finally call the partial view in the main view using “Html.RenderPartial” function and pass the view name in the function as shown in the below code snippet.

<body>
<div>
<% Html.RenderPartial("MyView"); %>
</div>
</body>

Also ensure that the partial view is in the same folder where your main view is. In case it’s not then you need to also pass the path in the “RenderPartial” function. You can see in the below figure I have moved the partial view in the main “Views” folder.

One more thing which is noticeable is that the icons for main view and partial are very different. You can see the yellow border in the partial view icon which does not exist in the main view icon.


Step 5:- Run the program and see the action.

Finally do a CNTRL + f5, put the proper controller path and see your hard work result. Below is the snapshot of how things should look like.


Article descriptionMVC ( Model view controller) videos MVC ( Model view controller) Article links
A simple Hello world ASP.NET MVC ( Model view controller) application. Hello word MVC Tutorial  video http://www.dotnetfunda.com/articles/article1297-how-to-create-a-simple-hello-world-aspnet-mvc-tutorial-no-1-.aspx
In this video we will see how we can share data between controller and the view using view data. MVC Tutorial video 2 Viewdata http://www.dotnetfunda.com/articles/article1310-how-to-pass-data-from-controllers-to-views-in-aspnet-mvc-tutorial-no-2-.aspx
In this article we will create a simple customer model, flourish the same with some data and display the same in a view. MVC Tutorial video 3 Simple model and view example http://www.dotnetfunda.com/articles/article1317-how-to-create-a-simple-model-in-aspnet-mvc-and-display-the-same-tutorial-.aspx
In this article we will create a simple customer data entry screen with some validation on the view. MVC tutorial video 4 Simple customer data entry screen. http://www.dotnetfunda.com/articles/article1388-how-to-create-a-simple-aspnet-mvc-data-entry-screen-tutorial-no-4-.aspx
This article will demonstrate how to expedite your MVC development process using HTML helper classes.NA http://www.dotnetfunda.com/articles/article1415-how-to-create-mvc-model-view-controller-views-faster-by-using-html-helper-.aspx
This article demonstrates how we can do unit testing in MVC (Model view controller) applications.NA http://www.dotnetfunda.com/articles/article1429-how-can-we-do-unit-test-in-mvc-model-view-controller-application-tutoria-.aspx
This article demonstrates how we can define custom routing in MVC applications.NA http://www.dotnetfunda.com/articles/article1485-what-is-mvc-model-view-controller-routing-tutorial-number-7-.aspx
This article demonstrates how we can validate data passed in MVC URL’s.NA http://www.dotnetfunda.com/articles/article1527-how-to-validate-data-provided-in-mvc-urls-mvc-tutorial-number-8-.aspx
In this article we will see how to create outbound URL’s in MVC applications.NA http://www.dotnetfunda.com/articles/article1596-how-to-create-mvc-outbound-urls-mvc-tutorial-number-9.aspx
Page copy protected against web site content infringement by Copyscape

Login to vote for this post.

Comments or Responses

Posted by: Samarmir on: 4/11/2012 | Points: 25
Thanks for a nice article, like to see that MVC is also being written about at this site.
And thanks for providing link to previous articles.
Posted by: Sruthimallinidi23 on: 6/9/2012 | Points: 25
hi,
Can you please post the article which will explain the how to render the parital view in different areas using Razor Syntax.
Posted by: Chiragkanzariya on: 11/29/2012 | Points: 25
Hello guys,

Check my blog for MVC articles and code http://www.chiragkanzariya.blogspot.in/

Login to post response

Comment using Facebook(Author doesn't get notification)