This article introduces partial view in ASP.NET MVC. When we need a common part of user interface at multiple pages in web application then we develop a partial view, hence partial view is regular view which can be used multiple times in an application and has file extension .cshtml. Sometimes we also use partial view to divide a web page in small parts such as header, footer and menu on Layout. Other examples are comments in blogging site, shipping and billing address in invoice in ecommerce site etc. A partial view is similar user control in web form application.
Using the Code
We create a sample application to understand partial view which showsa person's listing with their multiple addresses so we develop a partial view for address. We create two tables one is Person and another is Address which has one to many relationship as per figure 1.

Figure 1: Person and Address tables
To create partial view, we do right click on folder of views then expand Add option which has ‘MVC 5 Partial Page (Razor)’ as figure 2.

Figure 2: Partial View option
Now we click on partial page option then we get a window where we put the name of partial view as figure 3. Click on ok then partial view created in selected view folder.

Figure 3: Partial View Name Window
Now the following code snippet for _PersonAddress partial view which shows an address.
Using the Code
We create a sample application to understand partial view which showsa person's listing with their multiple addresses so we develop a partial view for address. We create two tables one is Person and another is Address which has one to many relationship as per figure 1.

Figure 1: Person and Address tables
To create partial view, we do right click on folder of views then expand Add option which has ‘MVC 5 Partial Page (Razor)’ as figure 2.

Figure 2: Partial View option
Now we click on partial page option then we get a window where we put the name of partial view as figure 3. Click on ok then partial view created in selected view folder.

Figure 3: Partial View Name Window
Now the following code snippet for _PersonAddress partial view which shows an address.
- @model EFOperation.Data.Address
- <address>
- @Model.Address1<br/>
- @Model.Address2<br/>
- @Model.City,@Model.Postcode
- </address>
- <hr/>
- [HttpGet]
- public ActionResult Index()
- {
- List < Person > people = new List < Person > ();
- using(OperationEntities context = new OperationEntities())
- {
- people = context.People.Include(p => p.Addresses).ToList();
- }
- return View("Index", people);
- }
- @model List
- <EFOperation.Data.Person>
- <h4>People List</h4>
- <hr />
- <table class="table-condensed table-bordered table-responsive">
- <thead>
- <tr>
- <th>Name</th>
- <th>Email</th>
- <th>Address</th>
- </tr>
- </thead>
- <tbody>
- @foreach (var person in Model)
- {
- <tr>
- <td>@person.Name</td>
- <td>@person.Email</td>
- <td>
- @foreach (var address in person.Addresses)
- {
- @Html.Partial("_PersonAddress", address)
- }
- </td>
- </tr>
- }
- </tbody>
- </table>
@Html.Partial
This Html.Partial renders partial view as an HTML string so we can store it in another string variable. It is string return type method so first it returns result as a string then renders result to response.
@Html.RenderPartial
This is also same as Html.Partial but main difference is that it renders result directly to response which means it writes response internally that’s why it is more efficient if the view has a large amount of HTML over @Html.Partial.
The output of the above implementation is as figure 4.

Figure 4: Output of partial view
Rendering a Partial View using Ajax
We can also call partial using ajax request, in other words partial view can be called by jQuery at client side. By rendering a partial view using ajax, we create an example in which user chooses person from dropdown list and respective address will be rendered below that.
You can get detail about this in article Rendering a Partial View and JSON Data Using AJAX in ASP.Net MVC .
We create a model which is used to pass the person's data from action method to view. The following code snippet for PersonDetailViewModel.
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Web.Mvc;
- namespace EFOperation.Models
- {
- public class PersonDetailViewModel
- {
- public PersonDetailViewModel()
- {
- Persons = new List < SelectListItem > ();
- }
- [Display(Name = "Person")]
- public int PersonId
- {
- get;
- set;
- }
- public List < SelectListItem > Persons
- {
- get;
- set;
- }
- }
- }
- [HttpGet]
- public ActionResult PersonDetail()
- {
- using(OperationEntities context = new OperationEntities())
- {
- PersonDetailViewModel model = new PersonDetailViewModel();
- model.Persons = context.People.ToList().Select(p => new SelectListItem
- {
- Text = p.Name,
- Value = p.PersonId.ToString()
- }).ToList();
- return View(model);
- }
- }
- @model EFOperation.Models.PersonDetailViewModel
- <h2>Person Detail</h2>
- <hr />
- <div class="form-group">
- @Html.LabelFor(m => m.PersonId, new { @class = "col-md-2 control-label" })
- <div class="col-md-10">
- @Html.DropDownListFor(m => m.PersonId, Model.Persons, new {@class="form-control" })
- </div>
- </div>
- <div class="form-group">
- <div id="addresses"></div>
- </div>
- @section Scripts{
- @Scripts.Render("~/Scripts/person-detail.js")
- }
- public PartialViewResult PersonAddressList(int id)
- {
- using(OperationEntities context = new OperationEntities())
- {
- List < Address > addresses = context.Addresses.Where(a => a.PersonId == id).ToList();
- return PartialView("_PersonAddressList", addresses);
- }
- }

Figure 5: Create Partial View
We write following code snippet for this partial view which shows multiple addresses for a person.
- @model List<EFOperation.Data.Address>
- @foreach(EFOperation.Data.Address address in Model)
- {
- <address>
- @address.Address1<br />
- @address.Address2<br />
- @address.City,@address.Postcode
- </address>
- <hr />
- }
- (function($)
- {
- function PersonDetail()
- {
- var $this = this;
- function intialize()
- {
- $("#PersonId").change(function()
- {
- var address = $("#addresses");
- address.html('');
- $.get("PersonAddressList",
- {
- id: $(this).val()
- }, function(data)
- {
- address.html(data);
- })
- });
- }
- $this.init = function()
- {
- intialize();
- }
- }
- $(function()
- {
- var self = new PersonDetail();
- self.init();
- })
- })(jQuery)

Figure 6: Partial view render using Ajax
We can also call partial view using load method of jQurey.


0 comments:
Post a Comment
Note: only a member of this blog may post a comment.