SIGN UP MEMBER LOGIN:    
ARTICLE

MVC 3 Razor And Partial View

Posted by Aravind BS Articles | ASP.NET MVC with C# December 07, 2011
In this article you will see how I use a partial view in MVC 3.
Reader Level:
Download Files:
 

How do I use a partial view in MVC 3?

Great!! Sometime, I don't know why, I stuck for small things. I was trying to load two partial views(MVC 3). But, after spending 6 hours I got to know how to load.

This retards my brain. I have been searching for good articles on MVC 3 & Partial views. But, I failed to find any.

So, Let us go in dept on MVC3 & Partial Views.

Partial views and User Controls are somewhat similar in purpose of usage. But the way in which they are used is totally different. I will explain about these in detail.

Step 1:

Create an ASP.NET MVC 3 project using VS 2010. I have named it as ARVPartialView.

Add one partiaview and name it as PartialIndex

MVC1.gif

Step 2:

I have created one module like PartialModel as given below.

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

namespace ARVPartialView.Models
{
    public partial class PartialModel
    {
        public string Name { get; set; }
        public int Actual { get; set; }
        public int Target { get; set; }
        public int Score { get; set; }
    }

    public partial class PartialModel
    {
        public List<PartialModel> lstPartialModel { get; set; }
 
    }
}


Step 3:

Now the time to add some values to this list. The solution structure looks like as given bellow.

Since I like render partial view over Home/Index, So, I have written one function in HomeController.

   public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            /// <summary>
            /// TODO:To get data and returning result to the index view..
            /// </summary>
            /// <returns></returns>
            return View(new PartialModel() { lstPartialModel = GetSampleData() });

        }

        public ActionResult About()
        {
            return View();
        }

        /// <summary>
        /// TODO:Function to add some data to list.
        /// </summary>
        /// <returns></returns>
        private List<PartialModel> GetSampleData()
        {
            List<PartialModel> model = new List<PartialModel>();

            model.Add(new PartialModel()
            {

                Name = "Aravind",
                Actual = 10000,
                Target = 12000,
                Score = 83
            });

            model.Add(new PartialModel()
            {
 
                Name = "Ram",
                Actual = 8000,
                Target = 14000,
                Score = 57
            });
            model.Add(new PartialModel()
            {
                Name = "Ajay",
                Actual = 50000,
                Target = 35000,
                Score = 143
            });

            return model;
        }
    }

Step 4:

In Index view, I have included partial view as given bellow. As like web usercontrol, partial view doest initiate any events while loading Just like initializecomponet etc. So, we have to pass the list to partial view in the parent page.

<!--
Author:Aravind
Module:PartialView.
Created On:07/12/2011
Modified By:
Modified On:

-->
@model IEnumerable<ARVPartialView.Models.PartialModel>
@using ARVPartialView.Models
    ViewBag.Title = "Aravind' PartialView Test !!";
}
<div data-role="page" data-title="Aravind's Partial View Test" data-add-back-btn="true" data-back-btn-text="Back">
    <div class="grid" style="margin-left: 5px;" id="grid">
        @if (Model != null)
        {
            <div class="grid">
                <table cellspacing="0" width="80%">
                    <thead>
                        <tr>
                            <th>
                                Name
                            </th>
                            <th>
                                Actual
                            </th>
                            <th>
                                Target
                            </th>
                            <th>
                                Score
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var itm in Model)
                        {
                            <tr>
                                <td align="center">
                                    @itm.Name
                                </td>
                                <td align="center">
                                    @itm.Actual
                                </td>
                                <td align="center">
                                    @itm.Target
                                </td>
                                <td align="center">
                                    @itm.Score
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
                <br />
                <br />
                <br />
                <br />
            </div>

        }
    </div>
</div>

Step 5:

Render partial view on Index view.

<!--
Author:Aravind
Module:PartialView.
Created On:07/12/2011
Modified By:
Modified On:

-->
@model ARVPartialView.Models.PartialModel
@{
    ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>
    To learn more about Hot Topics <a href="http://silverlightarvind.blogspot.com" title="Enjoy the Concept(Aravind)" />
    <a href="http://www.c-sharpcorner.com/Authors/aravindbenator/aravind-bs.aspx." title="About Me" />
    <div>
        @Html.Partial("IndexPartial", Model.lstPartialModel)
    </div>
</p>

Step 6:

Run the application.

MVC2.gif




 

Login to add your contents and source code to this article
share this article :
post comment
 

Dear Aravind it's a great work keep it up dude

Posted by Michell Johnson Dec 10, 2011

There was no proper tutorial on @HTML.Partial. that's why i have created basic one.

Posted by Aravind BS Dec 08, 2011

hi where can i find in detailed tutorials including syntaxes for methods like @html.partial,@html.actionlink,@html.display etc........ thnQ

Posted by thiru prakash Dec 08, 2011

thnQ for the reply i am creating my own apltn based on ur post now its working fine .......

Posted by thiru prakash Dec 08, 2011

i have uploaded source code.. so you can get it know and let me know, if any problem you face.

Posted by Aravind BS Dec 08, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor