Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C++
Tip/Trick

Creating a Partial View in MVC

Rate me:
Please Sign up or sign in to vote.
4.76/5 (12 votes)
10 Sep 2014CPOL2 min read 27.2K   6   6
In this tip, we will learn how to create a partial view and use that view in the parent view.

What is Partial View

A partial view is a view that can be rendered inside any other view, called the parent view. Partial view in MVC basically does the same job that user control does in web forms. Both are used for code reusability, so, a partial view can be called as a reusable view.

Partial view is generally used if we have reusable content in the page like header, footer, menu bar, left navigation bar, etc. It also divides the large page into smaller view so it is easy to code and maintain.

Create a View

Creating a view is as simple as adding a new page in the current web application.

Views-->Right click on module Add-->View

Creating a view

Create a Partial View

Creating a partial view is the same as creating a generally view. Here is how we can create a view.

Image 2

Pass Data to the View

This is the important part. Rendering a view means showing data in the UI.

The partial view has access to the same ViewData dictionary which is used by the view page. So, most of the time, we do not need to pass all the entities to the partial view. We can directly access the ViewData dictionary and show that in the partial view. But some time, if the viewData dictionary is empty, we need to fetch the data and pass that to the view.

For that, we need to define a controller and a model.

Create a Model

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PartialViewDemo.Models
{
    public partial class Book
    {
        public string BookName { get; set; }
        public int AuthorName { get; set; }
        public string ISBN { get; set; }       
    }
    public partial class Book
    {
        public List<Book> Books { get; set; }
    }
}

Create a Controller

Then, we have to define the Controller:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PartialViewDemo.Models;

namespace PartialViewDemo.Controllers
{
    public class BookController : Controller
    {
        public ActionResult Index()
        {            
            ViewBag.Message = "Here are the Book Details!";

            return View(new Book() { Books = GetDetails() });
        }
    private List<Book> Sampledetails()
        {
            List<Book> model = new List<Book>();

            model.Add(new Book()
            {
                BookName = "C#",
                AuthorName = "C#",
                ISBN = "1"
            });

            model.Add(new PartialModel2()
            {
                BookName = "Java",
                AuthorName = "Java",
                ISBN = "1"
            });
            model.Add(new PartialModel2()
            {
                BookName = "C++",
                AuthorName = "C++",
                ISBN = "1"
            });

            return model;
        }       
    }
}

We need to define the structure of the view here.

HTML
@model IEnumerable<PartialViewDemo.Models.Books>
@using PartialViewDemo.Models
   
        @if (Model != null)
        {
            <div class="grid">
                <table cellspacing="0" width="80%">
                    <thead>
                        <tr>
                            <th>
                                BookName
                            </th>
                            <th>
                                AuthorName
                            </th>
                            <th>
                                ISBN
                            </th>
                           
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model)
                        {
                            <tr>
                                <td align="left">
                                    @item.BookName
                                </td>
                                <td align="left">
                                    @item.AuthorName
                                </td>
                                <td align="left">
                                    @item.ISBN
                                </td>
                                
                            </tr>
                        }
                    </tbody>
                </table>
                
            </div>
        }

If we will check the create a strongly typed view from the add view window while creating the view, then we do not need to define the view. In that case, automatically index, Edit, Details, Delete, Create view will be added to the project.

Finally, we need to render the partial view in the parent view:

HTML
<div>
        @Html.Partial("PartialView1", Model.partialModel)
</div>

License

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


Written By
Software Developer (Junior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
mathangi.r4-Jan-15 19:22
mathangi.r4-Jan-15 19:22 
GeneralMy vote of 2 Pin
gicalle7516-Sep-14 21:43
professionalgicalle7516-Sep-14 21:43 
GeneralWell done! Pin
David NMN Hill16-Sep-14 5:27
David NMN Hill16-Sep-14 5:27 
GeneralRe: Well done! Pin
IpsitaMishra16-Sep-14 6:45
IpsitaMishra16-Sep-14 6:45 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun11-Sep-14 0:29
Humayun Kabir Mamun11-Sep-14 0:29 
GeneralRe: My vote of 5 Pin
IpsitaMishra11-Sep-14 0:36
IpsitaMishra11-Sep-14 0:36 

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

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